3

Background:

I am reading code written by someone else, and I am fairly new to C++ programming. When I look at the classes written by that person, and the corresponding member functions, I get confused with the usage of the this pointer. In some member functions this is used and in others not.

Why is that the case?

I know it is a very common confusion for the ones who start doing C++ recently.

Code Snippets:

The class:

class InitTable {
public:
    InitTable();
    virtual ~InitTable();

    void clearTable();
    void addEntry(std::string sumoID);
    void deleteEntry(std::string sumoID);
    InitEntry* getEntry(std::string sumoID);
    IPv4Address getEntryIPaddress(std::string sumoID);

protected:
    std::map<std::string, InitEntry*> table; 
};

Member function (with this):

void InitTable::clearTable()
{
    this->table.clear();
}

Member function (without this):

void InitTable::deleteEntry(std::string sumoID)
{
    InitEntry* ie = getEntry(sumoID);
    if (ie != NULL)
    {
        table.erase(sumoID);
        delete ie;
    }
}

Question:

Note that in void InitTable::clearTable() , this->table.clear() is used and in void InitTable::deleteEntry(), table.erase() only table without this is used.

void InitTable::clearTable()
{
    table.clear();    // no "this"
}

What is the trick in here? What would be the behaviour if this->table.erase() would be used instead.

void InitTable::deleteEntry(std::string sumoID)
{
    InitEntry* ie = getEntry(sumoID);
    if (ie != NULL)
    {
        this->table.erase(sumoID);   // "this" added
        delete ie;
    }
}

As I said, I'm a bit of n00b so a thorough description with minimal example would be very helpful.

cross
  • 1,018
  • 13
  • 32
  • 1
    Unless the use of `this` resolves a name clash (if the member is shadowed by, say, a function parameter), it is mostly a matter of taste. Personally, I always `this`-qualify member access. There may be good reasons to do the opposite but using one style consistently is almost certainly a good idea. – 5gon12eder Dec 02 '14 at 20:48
  • so basically in this case both ways would be fine with `this` and without it? – cross Dec 02 '14 at 20:50
  • Yes, the `this` is not required here, except by personal taste, perhaps. – 5gon12eder Dec 02 '14 at 20:53
  • `this` can also be used if you're returning a reference of the calling object – 김선달 May 26 '20 at 08:59

1 Answers1

3

It is never required inside a normal function, unless there is a parameter with the same name as a member. In a constructor you can use an initalizer list to prevent ambiguity. The use of a this pointer might be required when you use templates.

tillaert
  • 1,797
  • 12
  • 20
  • The most common reason to use `this` is to pass in to other functions where you need "pointer to this object". JavaScript, by contrast, insists on requiring this. – tadman Dec 02 '14 at 20:47
  • 1
    What if the class is of a template class? You can use `this->x` to make the expression dependent on the template types. – rodrigo Dec 02 '14 at 20:48
  • 2
    *never required* is not quite true. It is required when you're referring to a member of a base class template for instance. – Praetorian Dec 02 '14 at 20:50
  • 1
    _"... unless there is a parameter with the same name as a member"_ In the member initializer list of a constructor, the (parameter) names can be used unambiguously. – πάντα ῥεῖ Dec 02 '14 at 20:55
  • For expansion on Praetorians and rodrigos comments I found this http://stackoverflow.com/questions/7908248/in-a-templated-derived-class-why-do-i-need-to-qualify-base-class-member-names-w – Captain Giraffe Dec 02 '14 at 21:03