December 4th, 2013, 07:01 AM
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
Now, at one point we have the following assignments:
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 is left empty, but is set to contain values of type double? Any help understanding this greatly appreciated!
u = T();
rho = (T)1;
December 4th, 2013, 08:44 AM
u = T(); // u 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] are essential for python code and Makefiles!
December 4th, 2013, 03:29 PM
That first one,
u = 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 = 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.
December 4th, 2013, 04:04 PM
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:
using namespace std;
double x = double() ;
double rho = (double)1;
cout << "x = " << x << endl ;
cout << "rho = " << rho << endl ;
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.
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:
double x = 0.0 ;
double rho = 1.0 ;
int x = 1 ;
double rho = static_cast<double>(x) ;
December 4th, 2013, 04:21 PM
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:
Originally Posted by dwise1_aol
template<typename T> T default_object()
return T() ; // x with default initialisation to type T
double x = default_object<double>() ;
C y = default_object<C>() ;