Overloading identifier

K

Kira Yamato

Just curious. Does the C++ standard say anything about reusing the
same identifier for variables/class names/function names?

I tried this under g++4.0.1:

class a {};
class b {};

a b; // no problem here.
b c; // compiler complains "error: 'b' does not name a type"
class b c; // but no problem now if I use the keyword 'class'.

void a() {} // no problem here.

int main()
{
a e; // compiler complains "error: 'a' does not name a type
class a e; // but this is ok.

a(); // no problem here.

return 0;
}
 
J

James Kanze

Just curious. Does the C++ standard say anything about
reusing the same identifier for variables/class names/function
names?

Yes. In general, you can't have more than one declaration for a
name in a given scope, unless all of the names refer to
functions or function templates. (There's also a special rule
which allows an class name to be declared in the same scope as
other names, but that's just there for C compatibility, and is
best ignored.)
I tried this under g++4.0.1:
class a {};
class b {};
a b; // no problem here.

That's because of the special rule for C compatibility. Don't
do it.
b c; // compiler complains "error: 'b' does not name a type"

That's because when there is more than one declaration, and one
is for a class (or enum), then the compiler ignores it unless it
appears immediately after a "class specifier" (i.e. class,
struct or union) or enum. Here, b is a variable, and not the
name of a type.
class b c; // but no problem now if I use the keyword 'class'.

After a class specifier, there are special lookup rules. Again,
for reasons of C compatibility---don't write code depending on
them unless your goal is obfuscation.
void a() {} // no problem here.
int main()
{
a e; // compiler complains "error: 'a' does not name a type
class a e; // but this is ok.
a(); // no problem here.
return 0;
}

Same principles. After a class specifier or enum, the compiler
finds the name of the class; otherwise, it finds the name of
whatever else. If there is a conflict, of course. The best
policy is to not have a conflict.
 
K

Kira Yamato

Yes. In general, you can't have more than one declaration for a
name in a given scope, unless all of the names refer to
functions or function templates. (There's also a special rule
which allows an class name to be declared in the same scope as
other names, but that's just there for C compatibility, and is
best ignored.)




That's because of the special rule for C compatibility. Don't
do it.


That's because when there is more than one declaration, and one
is for a class (or enum), then the compiler ignores it unless it
appears immediately after a "class specifier" (i.e. class,
struct or union) or enum. Here, b is a variable, and not the
name of a type.


After a class specifier, there are special lookup rules. Again,
for reasons of C compatibility---don't write code depending on
them unless your goal is obfuscation.




Same principles. After a class specifier or enum, the compiler
finds the name of the class; otherwise, it finds the name of
whatever else. If there is a conflict, of course. The best
policy is to not have a conflict.

Thanks for the detail explanation. Certainly I will take your advice
on not doing this. I was just being curious what the rules are.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,516
Latest member
TobiasAxf

Latest Threads

Top