Archived

This topic is now archived and is closed to further replies.

underscore

This topic is 5506 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m working on some of the scripting tutorials that are on the Gamedev site. I''m porting them over to java because thats what I have to use at school, and I only have basic experience with c++. basically what I''m wondering is why some of the variables and such have an underscore infront of them. Some examples of this are:
  
1. class Instruction
2. {
3. public:
4.   Instruction(opcode code) : _code(code){}
5.   opcode Code() const         { return _code; }
6. private:
7.   opcode_code;
8.   //char*_data;  // additional data, currently not used

9. };
  
the lines in question are 4 and 5. Also I was wondering about that colon on line 4. Any help is greatly appreciated.

Share this post


Link to post
Share on other sites
They are usually a reminder that those variables are private, and they usually help work around clashes and allow you to name a function after them. ie, x->collision_time(); returning _collision_time. I perfer x_; myself. Some people use m_x for all members as well.

Share this post


Link to post
Share on other sites
The underscore itself doesn''t actually mean anything to the compiler, BTW. It''s just part of the variable''s name like any other alphanumeric letter.

Share this post


Link to post
Share on other sites
Oh, and that ": _code(code)" thing is kinda like saying super(...) in Java, cept in C++ you can use it to construct members also. ": _code(code) {..." translates to " {_code = code;}"

Peace,
Doc

Share this post


Link to post
Share on other sites
Underscores shouldn''t be used in your code, C++ programmers, as identifiers beginning with underscores are reserved for implementers (eg STL private/hidden classes, etc). You never know what could cause a name conflict (see <xmemory> section).

quote:
Original post by aggregate
They are usually a reminder that those variables are private, and they usually help work around clashes and allow you to name a function after them.

One alternative is to utilize case to distinguish between member variables and functions.

quote:
ie, x->collision_time(); returning _collision_time. I perfer x_; myself. Some people use m_x for all members as well.

I prefer this->x when I have a parameter and a private variable both named x. I know, it''s silly.

Share this post


Link to post
Share on other sites
quote:

... as identifiers beginning with underscores are reserved for implementers...

Is this apply to all underscored var?? I thought the reserved are in the form of an underscore followed by a capital letter.
eg.
_Tx -> this is reserved
_tx -> this shouldn''t be (is it?)

Share this post


Link to post
Share on other sites
quote:
Original post by Oluseyi
Underscores shouldn''t be used in your code, C++ programmers, as identifiers beginning with underscores are reserved for implementers (eg STL private/hidden classes, etc). You never know what could cause a name conflict (see section).

[quote]Original post by aggregate
They are usually a reminder that those variables are private, and they usually help work around clashes and allow you to name a function after them.

One alternative is to utilize case to distinguish between member variables and functions.

quote:
ie, x->collision_time(); returning _collision_time. I perfer x_; myself. Some people use m_x for all members as well.

I prefer this->x when I have a parameter and a private variable both named x. I know, it''s silly.

I picked up most of Scott Meyers'' coding conventions when I was beginning C++. Basically, it''s "Add a trailing underscore to private members." Anyway, to each his own, right. Height of the importance is that things are coherent.

Share this post


Link to post
Share on other sites
quote:
Original post by DerekSaw
Is this apply to all underscored var?? I thought the reserved are in the form of an underscore followed by a capital letter.
eg.
_Tx -> this is reserved
_tx -> this shouldn''t be (is it?)



quote:
C++ Standard, §17.4.3.1.2 Global names

1) Certain sets of names and function signatures are always reseved to the implementation :

- Each name that contains a double underscore (__) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.

- Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace165.



This forbids you from having global symbols (types, functions, variables, constants ... ) starting with an underscore - locals and members are theoretically fine. In practice, it is safer to abstain from defining any name starting with an underscore, as some implementations might use them - e.g. Visual C++ declares non-ANSI 89 C functions ( _itoa, _snprintf ... ) with a prepended underscore.

In short - just don''t.

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]

Share this post


Link to post
Share on other sites