Identifier overlaps

S

Samee Zahur

Hi,

How much 'identifier overlapping' is allowed in C++? I mean, can a
variable and a function have the same name in the same namespace/scope?
Can a member function be something like this:
int val(){return val;}
where val is a private int?


Samee
 
P

Phlip

Samee said:
How much 'identifier overlapping' is allowed in C++?

Please carefully name things, so you never need to learn these rules. For
example, all methods should have verbs, and all member data could have m_.

Ones C++ Sane Subset should be much narrower than what the language permits.
 
S

Siemel Naran

Samee Zahur said:
How much 'identifier overlapping' is allowed in C++? I mean, can a
variable and a function have the same name in the same namespace/scope?
Can a member function be something like this:
int val(){return val;}
where val is a private int?

Assuming the val you want to return is a member variable, then the above is
illegal. But to make it legal you can do

int val(){return this->val;}
 
S

Siemel Naran

Phlip said:
Samee Zahur wrote:

Please carefully name things, so you never need to learn these rules. For
example, all methods should have verbs, and all member data could have m_.

This is a coding standard, pretty common, but not a rule. Usually I use d_
for normal data and m_ for mutable members, though many people use m_ for
everything.
Ones C++ Sane Subset should be much narrower than what the language
permits.

Why?
 
V

Victor Bazarov

Siemel said:
Assuming the val you want to return is a member variable, then the
above is illegal. But to make it legal you can do

int val(){return this->val;}

How is that going to be legal? What compiler allows it?

V
 
B

ben

Assuming the val you want to return is a member variable, then the above is
illegal. But to make it legal you can do

int val(){return this->val;}

To make "this" a legal entity, the above function definition must be a
member function in a class definition, which has a member variable called
"val" of type int, which conflicts the name of the member function defined
above.

ben
 
S

Siemel Naran

Victor Bazarov said:
Siemel Naran wrote:

How is that going to be legal? What compiler allows it?

What if val is a public or protected member of the base class? Sorry, I
didn't try it on any compiler.
 
N

Niels Dybdahl

How much 'identifier overlapping' is allowed in C++?
Please carefully name things, so you never need to learn these rules. For
example, all methods should have verbs, and all member data could have m_.

And try to avoid underscores in member names. They do not differ very much
from white space and so they make the source harder to read.

Niels Dybdahl
 
V

Victor Bazarov

Siemel said:
What if val is a public or protected member of the base class?

Then 'val' in the derived class hides the base class. You need
to use baseclassname::val to resolve it.
Sorry, I didn't try it on any compiler.


You should, you know...
 

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,202
Messages
2,571,057
Members
47,666
Latest member
selsetu

Latest Threads

Top