Yesterday, I modified some C++ code and faced a quite weird problem. Long story short, I changed the code that declares an instance of a class from something like
/* declares 'a' to be an instance of 'ObjectA' and
the constructor takes a pointer to a variable as the parameter */
ObjectA a(&hello);
/* no problem here */
a.DoSomething();
to
/* similar to the above but the constructor now takes an reference of ObjectB as the parameter and ObjectB's constructor in turn takes a pointer to 'hello' as the parameter */
ObjectA a(ObjectB(&hello));
/* By this way of declaration, this line cause C2228 error, i.e. 'a' is not a class or struct! Why ? */
a.DoSomething();
I tried to compile the same code with g++. Surprisingly, a similar error was reported! What's wrong ? Both compilers didn't complain on the declaration line, which means the syntax is correct. However, base on the error reported, although the 'declaration' was syntactically correct, both compilers couldn't (or didn't) determine that I was declaring a variable 'a'!
(閱讀全文)





