A question in the name lookup.

W

Wayne Shu

see the following code from the book C++ Templates The Complete Guide

#include <iostream>

int C; // (1)

class C // (2)
{
private:
int i[2];
public:
static int f(){
return sizeof(C);
};
};

int f()
{
return sizeof(C);
}


int main()
{
std::cout << "C::f() " << C::f() << ","
<< "::f() " << ::f() << std::endl;
return 0;
}

why the result of the program is
C::f() 8,::f() 4

why the two names C are not ambiguous.
if aren't ambiguous ,why the name C(2) doesn't hide the name C(1).
why in function f(), the C refer to the C(1).
 
V

Victor Bazarov

Wayne said:
see the following code from the book C++ Templates The Complete Guide

#include <iostream>

int C; // (1)

class C // (2)
{
private:
int i[2];
public:
static int f(){
return sizeof(C);
};
};

int f()
{
return sizeof(C);
}


int main()
{
std::cout << "C::f() " << C::f() << ","
<< "::f() " << ::f() << std::endl;
return 0;
}

why the result of the program is
C::f() 8,::f() 4

why the two names C are not ambiguous.

Where? In the 'main', when you write 'C::' you cannot mean the 'int'
variable, can you? The name preceding the scope resolution operator
is always looked up among classes and namespaces.

In the 'C::f' member, 'C' _hides_ the 'int' variable declared outside
of the class itself. So, 'sizeof(C)' in 'C::f' has no ambiguity.
if aren't ambiguous ,why the name C(2) doesn't hide the name C(1).

It does. But only in the nested scope -- in the class itself.
why in function f(), the C refer to the C(1).

Because outside the scope of the class C, the _class_ C has to be
referred to with 'class C' syntax (specifically for disambiguation
purposes). Or, in a particular context ("blah::") only names of
classes and namespaces are looked at.

The co-existence of variable 'C' and 'class C' is allowed for
compatibility with the C language, where you are allowed to have
both 'X' object and 'struct X', and no ambiguity exists because
the latter always has to be supplied the 'struct' keyword.

V
 
G

Grizlyk

Wayne said:
#include <iostream>

int C; // (1)

class C // (2)

If you have no C++ compiler integrated to your mind do not do like this. Use
prefix to make differences between classes, types, namespaces and variables,
short prefix can help you to understand what do you want.

For example prefixes: "T","N" and "t_"

T???: Tclass //to avoid Tclass.hello();
N???: Nnamespace //to avoid new Nnamespace;
t_??: t_POD //C-compatible structures
????: variable //to avoid new variable; or class x: public variable{};
 
D

Dave Rahardja

If you have no C++ compiler integrated to your mind do not do like this. Use
prefix to make differences between classes, types, namespaces and variables,
short prefix can help you to understand what do you want.

For example prefixes: "T","N" and "t_"

T???: Tclass //to avoid Tclass.hello();
N???: Nnamespace //to avoid new Nnamespace;
t_??: t_POD //C-compatible structures
????: variable //to avoid new variable; or class x: public variable{};

Prefixes are just one way to disambiguate symbols. The OP may want to review
C++'s symbol lookup rules and choose a method of disambiguation for his code.
I personally use capitalization (initial cap for types only) and judicious use
of namespaces with good results.

I have never had to use Hungarian Notation or any other "type- or
scope-dependent name decoration" techniques on my code, and I am forever
thankful for it. I prefer completely unadorned variable names for their
clarity.

-dr
 

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
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top