Jump to content
  • Advertisement
Sign in to follow this  
_Sigma

this- ----Is it needed?

This topic is 4944 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

this is jyst the instance of the current class. It can be usefull. For instance you may want to pass the instance itself in a function call:

void MyClass::Something()
{
DoSomething(this);
}

or even (not good style) self-deletion:

delete this;

-CProgrammer

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by _Sigma
So why m_ ? why not Cname, or? or is it just personal preferance?


Cname is used usually for prefixing class names. m_ is used to prefix member variables of a class. I like it, it makes things nice and clear:


class Thing
{
public:
Thing(int hp)
:m_hp(hp)
{}

void SetHP(int hp)
{
m_hp = hp;
}

private:
int m_hp;
};

Share this post


Link to post
Share on other sites
Quote:
Original post by Drag0n
Quote:
Original post by PnP Bios
I prefer it! I use it for clarity, and to trick intellisense into showing me the members of my class.


Does that mean you don't know what members your class has? ;)

I certainly don't, and even when I do Intellisense ensures everything gets misspelled correctly.

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by BittermanAndy
... or even just _.


No no no no no no no!!! (no!)

A simple underscore says nothing universally, and variables prefixed with it are reserved by the implementation in various scopes (one of the reasons I am disturbed by my IDE's insistance on using _FILENAME_TYPE_ as the default include guard macro name).

An underscore followed by a lowercase letter is reserved in the file scope.
An underscore followed by an uppercase letter or another underscore is reserved in all scopes [source].

m_ is a widely recognized prefix which also abbreviates exactly what it stands for - a member variable.

Quote:
Original post by _Sigma
So why m_ ?


It stands for Member (variable). Some people also use g_ for Global (variable). It is a matter of either personal preference, or to comply with guidlines set forth either by your employer or project that you are working on.

For what it's worth, I can't remember the last time I prefixed a variable name with m_. I prefer more descriptive names. For a vector, instead of m_data, m_size, m_allocated, etc, I'd use something similar to:

data, data_initialized, data_allocated

Note the repeated use of "data" in this case. It is clear that these variables describe how many elements of data have been initialized and allocated, respectively. The fact that size() simply returns data_initialized, and capacity() data_allocated, is simply a coincidence of the implementation.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!