b.g(); | // Error? |
}
The ambiguity in the example code is whether b is declared as:
•A function with one argument (named p) returning an object of type B.
•An object of type B initialized with a temporary object of type A.
HP C++ compiles this code successfully and assumes b is an object. Compiling the code with HP aC++ generates the following error:
Error: File “objDeclaration.c”, Line 5
Left side of ‘.’ requires a class object; type found was a function ‘B (A)’. Did you try to declare an object with a nested constructor call?
Such a declaration is interpreted as a function declaration B b(A) [File “objDeclaration.c, Line 4].
Modify the code as shown below to successfully compile it with both compilers.
struct A {A(int);};
struct B {B(const A &); void g();};
void | f(int p) { |
|
|
|
B | b = A(p); | // | declaration | of object |
b.g(); | // | method call |
| |
} |
|
|
|
|
Overloaded Operations ++ and
You must use the overloaded operations ++ and
Example:
class T { public:
T();
const T& operator++ ();
};
int main () {
Tt;
t++;
}
Compiling the above code with HP C++ generates the following warning:
CC:"pre.C", Line 8: warning: prefix
Compiling the code with HP aC++ generates an error like the following:
Error 184: File “pre.C”, Line 8
Arithmetic or pointer type expected for operator ‘++’; type found was ‘T’.
To compile the code with HP C++ or HP aC++ use the following class definition:
class T { public:
T();
const T& operator++ (); // prefix old style postfix definition const T& operator++ (int); // postfix
};
Reference Initialization
Illegal reference initialization is no longer allowed. In HP C++, a warning is generated stating that the initializer for a
216 Migrating from HP C++ (cfront) to HP aC++