C++ Member Variables

Julian picture Julian · Apr 17, 2012 · Viewed 43.1k times · Source

Consider the following class:

class A
{
  A();
  int number;
  void setNumber(int number);
};

You could implement 'setNumber' in 3 ways:

Method 1: Use the 'this' pointer.

void A::setNumber(int number)
{
  this->number = number;
}

Method 2: Use the scope resolution operator.

void A::setNumber(int number)
{
  A::number = number;
}

Method 3: Instead, denote all member variables with 'm' or '_' (this is my preferred method).

void A::setNumber(int number)
{
  mNumber = number;
}

Is this just personal preference, or is there a benefit to choosing a particular method?

Answer

Kerr picture Kerr · Apr 17, 2012

This is mostly a personal preference, but let me share my perspective on the issue from inside a company where many small games are being made simultaneously (and so there are many coding styles being used around me).

This link has several good, related, answers: Why use prefixes on member variables in C++ classes

Your option 1:

void A::setNumber(int number)
{
  this->number = number;
}

First off, many programmers tend to find this cumbersome, to continually type out the ' this-> '. Second, and more importantly, if any of your variables shares a name with a parameter or a local variable, a find-replace designed to say, change the name of 'number' might affect the member variables located in the find-replace scope as well.

Your option 2:

void A::setNumber(int number)
{
  A::number = number;
}

The problem I've run into with this, is that in large classes, or classes with large functions (where you cannot see the function or the class is named unexpectedly), the formatting of A::(thing) looks very much like accessing a part of a namespace, and so can be misleading. The other issue is the same as #2 from the previous option, if your names are similar to any variables you're using there can be unexpected confusion sometimes.

Your option 3:

void A::setNumber(int number) 
{
  mNumber = number;
}

This is the best of those three presented options. By creating (and holding to!) a syntax that involves a clear and meaningful prefix, you not only create a unique name that a local (or global) variable won't share, but you make it immediately clear where that variable is declared, regardless of the context you find it in. I've seen it done both like this ' mVariable ' and like this 'm_variable' and it mostly depends upon if you prefer underscores to uppercase concatenation. In addition, if your style tends to add things like ' p 's on for pointers, or ' g 's on for globals, this style will mesh well and be expected by readers.