this- ----Is it needed?

Started by
22 comments, last by MaulingMonkey 18 years, 8 months ago
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
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;};
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
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.

This topic is closed to new replies.

Advertisement