I have the following line of code:
double *resultOfMultiplication = new double(*num1 * *num2);
How does the compiler know which * is used for derefencing and which * is used for multiplication?
Also, and probably a more important question is in this case is double a primitive (like in Java) or an object? If it’s a primitive how can I create a new one?
Dereference comes first, so
*num1 * *num2gets parsed as(*num1) * (*num2), which is unambiguous.*resultOfMultiplicationis not parsed as dereference because it is a variable definition. In such a context, the compiler expects a data type followed by an identifier so the asterisk is unambiguous.Primitive data types are still objects in C++. If you use
newon a primitive type, all that happens is that enough memory in the free store to hold the object is allocated and its address returned to you. This is unlike ‘normal’ variables (i.e.double t;), which are of automatic or static storage duration.