#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    5
    Rep Power
    0

    Confused about the use of parentheses in c++


    Hey, I'm getting mightily confused about the use of parentheses for various type declarations, so I was wondering if someone here wanted to give me a hand. I'm sure it doesn't make any difference, but just to be on the safe side I'll mention that at the beginning of the program we have
    Code:
    typedef double T
    Now, at one point we have the following assignments:
    Code:
    u[1] = T();
    rho = (T)1;
    u is an array and rho is a simple scalar value. But I don't quite understand what's going on with the parentheses here? Also, am I getting it right that u[1] is left empty, but is set to contain values of type double? Any help understanding this greatly appreciated!
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Code:
    u[1] = T(); // u[1] is probably assigned (double)0  The declaration of u determines the data type of the values it stores.  T() should make a new object of type T. 
    rho = (T)1; //  The parenthesized type in this instance is a cast.  "convert the int 1 to type T then assign it to the variable rho"
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    That first one,
    u[1] = T();
    doesn't appear to make any sense since T is neither a class nor a function.

    Please write a short compilable program that illustrates the usages that you have questions about. Be sure to comment each line, explaining what you are trying to do in that line.

    Also compile that program with warnings turned on and up.

    Post your code in a message here along with the error and warning messages that the compiler generated.

    For example, when the compiler encounters that line,
    u[1] = T();
    , I am willing to bet that it will assume that T() is a function call, but since you had not provided it with a function of that name nor with a prototype for a function of that name, it will generate a warning that it had to declare that function implicitly with default properties (ie, that the function would take one int argument and return an int value).

    Perform that exercise and display your results.
  6. #4
  7. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    When asking a question it is always a good idea to simplify it to its essence and not obfuscate it with extraneous detail that has nothing to do with the question in hand. In the case the typedef is irrellevant as it the use of the array u rather that a single variable.

    Consider this example:

    C++ Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
        double x = double() ;
        double rho = (double)1;
     
        cout << "x = " << x << endl ;
        cout << "rho = " << rho << endl ;
     
       return 0;
    }


    The expression double x = double() ; creates a temporary value by calling teh default constructor of the type double, which yields a floating point zero value.

    The expression double rho = (double)1; casts the integer value 1 to the double floating-point value 1.0.

    Now another thing to remember is that not all example code you observe will be an example of good practice or even serve any purpose. These examples are almost entirely pointless.

    In the first case, invoking the default constructor of a built-in discrete type serves no purpose, the facility to even do it is only for syntactic and semantic consistency with class types; there is no real constructor here. In the second instance, casting an integer literal has no benefit over using a floating-point literal.
    Code:
    double x = 0.0 ;
    double rho = 1.0 ;
    Casting does serve some purpose when the expression on the right-hand side of the assignment is variable, but in C++ you should use C++ rather than C style casts:
    Code:
    int x = 1 ;
    double rho = static_cast<double>(x) ;
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,109
    Rep Power
    1802
    Originally Posted by dwise1_aol
    That first one,
    u[1] = T();
    doesn't appear to make any sense since T is neither a class nor a function.
    The expression T() invokes the default constructor of the type T. It the case of a built-in type it serves little purpose over simply assigning 0.0, but if the typedef were changed to a class type, the code would still work and would do useful work - none more than simply instantiating an object, but it allows switching between a built-in type and a class type while providing types specific initialisation for both. The technique is perhaps more usefully used in C++ template code:

    Code:
    template<typename T> T default_object()
    {
        return T() ;  // x with default initialisation to type T
    }
    Code:
    class C
    {
        public :
            C() ;
    } ;
    
    double x = default_object<double>() ;
    C y = default_object<C>() ;

IMN logo majestic logo threadwatch logo seochat tools logo