[comp.lang.c++] new foo: syntax error?

wsmith@m.cs.uiuc.edu (03/08/89)

This example was discovered when CC gave a "syntax error" message
that was very difficult to decode to find the source of the problem.

The problem stems from the property that identifiers can have different 
meaning depending on their syntactic location when the identifier is 
both a class name and a variable name.

In a distilled version of the code that caused the initial gotcha,
on line 11 below, foo is both a member and a class.  Should
the constructor for class foo with "new foo" be allowed? CC says no and 
reports "syntax error."  I can read the C++ book and believe that this is 
"correct."  (CC's fix is to say "new class foo", which is not necessary in 
g++.)

If it is impossible for the next token after new to be anything but a
class name, why should it be confused with a member or variable of the
same name?  (I don't think a program can take the address of a constructor.
Even if you could, using such a pointer could be unambiguously distinguished
from a use of a class name directly.)  

Bill Smith
wsmith@cs.uiuc.edu
uiucdcs!wsmith

------------------------------------------------------------
class foo{
	};

class bar {
	int foo;
	void function(void);
};

void bar::function()
{
/*###11 [CC] error: syntax error%%%*/
	class foo * tmp1 = new foo;
}
------------------------------------------------------------