Link to home
Start Free TrialLog in
Avatar of takkie
takkie

asked on

does the underscore or double underscore mean something in c++?

there are some variables that have some leading underscores (_) or (__) infront of a variable...does that mean anything?

I am not looking for answers like, leading underscores for a variable name is valid. I understnad that...but i have seen some codes, for example, look below.


class CMyClass  
{
public:
     __declspec(dllexport)  CString SayHello (CString strName);
     __declspec(dllexport)  CMyClass();
     __declspec(dllexport)  virtual ~CMyClass();

};

why do they design declspecto have 2 leading underscores? There are some with just 1 leading underscores...

from the books i read for c++, i dont see any explanation of this...
anyhelp?

by the way, i asked a question last week, but didnt close it yet, i have an answer to accept...but i cant find that question...how do i see what are the qeustions i have asked in experts-exchange.com??

thanks,
takkie
Avatar of cybeonix
cybeonix

If I recall correctly, the underscores are placed there to help the linker when it compiles the object.   And if I'm not mistaken, underscores indicate whether arguments are passed on the stack or in registers (maybe not all compilers).  Sometimes the function will be prefixed by an @ symbol as well, however that ends up in the object code and isn't valid for declaring functions.

Either way, declaring variables with underscores etc is valid.  I generally use it myself to avoid conflict with the API I'm using.  Some operating systems will have a structure named identically to something I want to use in my application.  I'll generally declare them as  struct __mystructure  just to avoid complications.
Avatar of takkie

ASKER

hi cybeonix,

i dont really understand what you mean, can you rephrase it in a simple wordings?

thanks.
tak
The C and C++ standards reserve symbols beginning with double underscores for implementations and they should not be defined by user programs (they can be used, but not defined). This allows vendors, writers of application libraries, etc a way to create symbols that have some assurance of not conflicting with users' programs.
In simple term:

Double __ : reserved identifiers that are compiler dependent (introduced by vendor, not by standard C++)
ex: __fastcall (BCB); __declspec (VC++)
Single _  : reserved identifiers that are platform (machine/operating system) dependent
ex: _read, _geninterrupt (DOS/windows); _pthread (linux)

But this is only convention, there are some violation :)
 
The C and C++ language standards reserve identifiers starting with either two underscores or an underscore followed by an upper-case letter for the compiler implementation.

The C++ standard further says "Each name having two consecutive underscores is reserved to the implementation for use as a name with both extern "C" and extern "C++" linkage."  This specification may be intended to apply only when the two underscores are at the beginning of the name, but it doesn't say that.

Otherwise, the standards don't specify any difference between names that start with two underscores and names that start with one and an upper-case letter.

To find the questions you have asked, click on your name "takkie" in the From: field of your question on this page.  That will take you to your Member Profile, where all the questions you have asked are listed.
Avatar of takkie

ASKER

in other words, unless i WANT to define a variable as

int __counter = 2;

i dont have to worry about the __ or _, right? Becuase I dont have to create variables with _ or __ myself anyway, its a vendor specific kind of thing...am i correct?
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial