🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by wsmith@m.cs.uiuc Wed, 08 Mar 1989 05:39
new foo: syntax error?
#2723
Author: wsmith@m.cs.uiuc
Date: Wed, 08 Mar 1989 05:39
41 lines
1338 bytes

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;
}
------------------------------------------------------------
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads