underscores in class member names

K

Keith H Duggar

Thanks for the example Rolf.
int _Z4testv;

void test() {};

int main()
{
test();
}

The reason is that the internal mangled name for the function test is
_Z4testv, so there is a naming conflict with the global variable. The
error message looks like this:

/tmp/cckaLQYy.s: Assembler messages:
/tmp/cckaLQYy.s:13: Error: symbol `_Z4testv' is already defined

Personally, I never pollute the global namespace with underscore
names. I was mainly thinking of continuing to use __xyz to indicate
member variables. I tried using m_xyz for a while but the code didn't
look nearly as nice as with __xyz. Also, I tried using xyz_ as many
suggest but again __xyz was much easier on my eyes and I take it that
xyz__ would also be reserved (double underscore) is that correct?

However, your example seems to indicate that I could run into name
mangling problems even within a class namespace.

Thank you again, I will have to look into this more.

PS. What convention to you use for member variable names?
 
B

Bob Hairgrove

Personally, I never pollute the global namespace with underscore
names. I was mainly thinking of continuing to use __xyz to indicate
member variables. I tried using m_xyz for a while but the code didn't
look nearly as nice as with __xyz. Also, I tried using xyz_ as many
suggest but again __xyz was much easier on my eyes and I take it that
xyz__ would also be reserved (double underscore) is that correct?

However, your example seems to indicate that I could run into name
mangling problems even within a class namespace.

Don't forget that although your member names are within a namespace,
the ones we have been talking about often are not, so there is still
plenty of opportunity for clashes.
 
R

Rolf Magnus

Keith said:
Thanks for the example Rolf.


Personally, I never pollute the global namespace with underscore
names. I was mainly thinking of continuing to use __xyz to indicate
member variables. I tried using m_xyz for a while but the code didn't
look nearly as nice as with __xyz. Also, I tried using xyz_ as many
suggest but again __xyz was much easier on my eyes and I take it that
xyz__ would also be reserved (double underscore) is that correct?

Yes. Everything that contains two consecutive underscores, so both __xyz
and xyz__ are reserved.
However, your example seems to indicate that I could run into name
mangling problems even within a class namespace.

I'm not sure how likely it is that you can get into problems with your
above naming scheme, but my example at least shows that there might be
things you haven't been considering. I don't know of other internal
uses of reserved identifiers than standard library macros and name
mangling, but that doesn't mean there are none, and it also will depend
on the compiler. The simplest way to be sure still is by just not using
reserved identifiers.
Thank you again, I will have to look into this more.

PS. What convention to you use for member variable names?

xyz_
 

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,770
Messages
2,569,586
Members
45,088
Latest member
JeremyMedl

Latest Threads

Top