The 'this' pointer in the initialization list of the constructor
Asked Answered
I

2

28

I guess I am unable to understand why this is not working. I always thought that I can use 'this' pointer inside the constructor, but I never knew that I cannot use 'this' in the initialization list.

#include <iostream>

class A {
    public:
        int a;
        int b;
        A(int a = 0, int b = 0) : this->a(a), this->b(b) { }
        void print() {
        std::cout << a << ", " << b << std::endl;
    }
};

int main() {
    A a;
    a.print();
}

I am interested to know the details related to it.

Illgotten answered 12/1, 2017 at 14:19 Comment(4)
I've tagged "language lawyer": I'm not convinced my answer is correct.Susan
You're not inside the body of the constructor though, you're in the initializer list of the constructor.Shortfall
@DavidSchwartz b(this->a) would be just fine.Tengdin
Thank you so much for the question! Saved my time!Internalcombustion
S
38

Simply because there's no need, an initializer list can already disambiguate because its syntax is strict:

member(value)

So you can just change it to:

A(int a = 0, int b = 0) : a(a), b(b) {}

this->member is only really used when the programmer needs to help the compiler to disambiguate, for example, if your constructor would've looked like:

A(int a = 0, int b = 0) 
{ 
  // set local 'a' to itself
  a = a; 
}

Your A::a wouldn't have been initialized now, oops!

You would need this to help the compiler:

A(int a = 0, int b = 0) 
{ 
  this->a = a; // set A::a to local a.
}
Shemikashemite answered 12/1, 2017 at 14:22 Comment(3)
I'd like to add that using this to access members when ambiguity is an issue is more of a Java thing. In C++, your attributes should (usually) have a name like a_, which reduces the above line to a_ = a;. To quote the Google C++ Style Guide: Data members of classes (but not structs) additionally have trailing underscores.Engineering
@KonstantinĐ.: Note that the Google C++ Style Guide is not well-respected among C++ programmers, and should not be taken as representative of what the C++ community recommends.Circumvent
@KonstantinĐ. The value of prefix/suffix naming conventions are very subjective, and often unnecessary. That's why the this construct exist in the first place. I would recommend Clean Code by Bob Martin for a different perspective.Rosemari
T
31

this->a is grammatically invalid because it is a member-access expression, but only an identifier is allowed there (or a type specifier, for base classes).

From the C++ standard, [class.base.init],

mem-initializer-id:
      class-or-decltype
      identifier

Tengdin answered 12/1, 2017 at 14:54 Comment(1)
Right, and the "why" is .... because there would be absolutely no need to complicate the grammar to allow an added this->. In fact you'd then have to backtrack with a bunch of "... is ill-formed" rules to ban basically anything you'd just added but the added this->.Manned

© 2022 - 2024 — McMap. All rights reserved.