Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Member Variables

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?

like image 544
Julian Avatar asked Apr 17 '12 19:04

Julian


People also ask

What is a member variable in C?

A member variable is the variable you declare in a class definition. Its lifespan is inside that class only.It is Global Variable.It can be access by any function inside that same class.

What is member function in C?

Member functions are operators and functions that are declared as members of a class. Member functions do not include operators and functions declared with the friend specifier. These are called friends of a class. You can declare a member function as static ; this is called a static member function.

What is member variable in C sharp?

Member variables are the attributes of an object (from design perspective) and they are kept private to implement encapsulation. These variables can only be accessed using the public member functions.


1 Answers

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.

like image 84
Kerr Avatar answered Oct 14 '22 23:10

Kerr