473,466 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

A question in the name lookup.

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).

Jan 30 '07 #1
3 1250
Wayne Shu wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jan 30 '07 #2
Wayne Shu wrote:
>
#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{};

--
Maksim A Polyanin
Feb 3 '07 #3
On Sat, 3 Feb 2007 21:14:40 +0300, "Grizlyk" <gr******@yandex.ruwrote:
>Wayne Shu wrote:
>>
#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{};
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
Feb 3 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Srini | last post by:
I was reading the "Exceptional C++" of Herb Sutter. In an example, he mentions the following. // In some library header: namespace N { class C{}; } int operator+(int i, N::C) { return i+1; }...
5
by: David Rasmussen | last post by:
If I have a string that contains the name of a function, can I call it? As in: def someFunction(): print "Hello" s = "someFunction" s() # I know this is wrong, but you get the idea... ...
16
by: Michael | last post by:
Does someone have a reliable routine to get the domain name of the current user of a web page? Something like NSLookup. I have no problem getting their IP address, but I also want the domaine...
1
by: KK | last post by:
Hi, Is there any way to change the name of XML node? In the following xml, I want to change the node <FIELD_1> to <FIELD_ITEM> in all the places. Ex: <NewDataSet> <LOOKUP>...
0
by: KK | last post by:
Hi, Is there any way to change the name of XML node? In the following xml, I want to change the node <FIELD_1> to <FIELD_ITEM> in all the places. Ex: <NewDataSet> <LOOKUP>...
0
by: ketan | last post by:
Hi, While understanding name lookup using item 32 of EC++ ( Sutter ), i found error from gcc bit unclear. It will be great if anyone can help me. In short - compiler complains about...
9
by: cmk128 | last post by:
Hi Why put * in front of the function name in the declaration? int (*write)(struct inode *, struct file *, const char *, int); thanks from Peter (cmk128@hotmail.com)
9
by: Michael M. | last post by:
Hi all, I would like to know how to access the NT/2000/XP/2003 Name cache; what I mean by this is: Open a Command Prompt and.., C:\> C:\>IPCONFIG /DISPLAYDNS
3
by: Fraser Ross | last post by:
10.2/2 of the C++ standard says this: "The following steps define the result of name lookup in a class scope, C. First, every declaration for the name in the class and in each of its base class...
3
by: WaterWalk | last post by:
Hello. Consider the following two examples: class Test1(object): att1 = 1 def func(self): print Test1.att1 // ok class Test2(object): att1 = 1 att2 = Test2.att1 // NameError: Name Test2...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.