How to reference instance variable with the same name as a local variable in C ++?

1

In C ++, how can I reference an instance variable that has the same name as a global variable? For example, in a set method of the following class:

class Person
{
private:
    string name;

public:
    void setName(string name) 
    {
        //Em java eu faria: this.name = name;
    }

    string getName()
    {
        return name;
    }
};
    
asked by anonymous 23.10.2017 / 00:09

1 answer

4

In C ++, keyword this exists. It is reserved for contexts where an object is having its method being executed.

This this behaves as a constant, which is a pointer to the class from which the method is running. Of course, it needs to be an instance method, static methods can not refer to the object itself.

  

To be more exact, this is a prvalue

In fact, every reference to an instance method or attribute within the object is treated as having an implicit call to this-> . In your example:

string getName()
{
    return name;
}

If used in a totally explicit way would be:

string getName()
{
    return this->name;
}

Among other functions, explicitly calling this is for disambiguation. For example, in its setter , there is a parameter named name , parameter name that "throws a shadow" over the instance variable name .

  

In English, it is said that "the parameter shadows the member with the same name"

To solve this situation, you can use this so that everything is unambiguous:

void setName(string name) 
{
  this->name = name;
}

(Anti-) C ++ Naming Standards

Because of such problems, some C ++ schools claim that instance variables should start with " _ ". Then you would use _name for the variable and name for the parameter. In general, your code would look like this (if it was to follow the guidelines of this school):

class Person
{
private:
    string _name;

public:
    void setName(string name) 
    {
        _name = name;
    }

    string getName()
    {
        return _name;
    }
};

I particularly find this pragmatic but ugly exit. I would not use it.

Another use would be in constructors; for the case of Person have a constructor that receives as an argument a string for the name:

Person::Person(string name) {
  _name = name;
}

But this style of coding does not use the more elegant features of C ++ to build objects.

Using initializer in constructor

Leaving the world where there is " _ " before the name of the internal variables, you can very well use initializers in the constructor. For example, in the constructor that receives a string and initializes the name attribute of the object:

Person::Person(string name) : name(name) {
}

In this syntax, the name attribute is initialized with the value in parentheses, which in this context is the name parameter. Generally, this initializer can be understood something like this:

<assinatura construtor> : <atributo> ( <valor de inicialização> )

Where:

  • <assinatura construtor> is the constructor signature ( Person::Person(string name) )
  • <atributo> is the name of the attribute that will be initialized (the first name , which comes before the parentheses)
  • <valor de inicialização> is the value that will be assigned to the attribute (in this case, the name within the parentheses, which corresponds to the constructor parameter)

Some additional search sources:

23.10.2017 / 04:31