As I understand it, when I read about this, a pointer is needed mostly when we use several objects of the same class. However, the question arises whether it is necessary to assign a value through a pointer in the default constructor or with parameters (below the example code

class A{ public: A(int in); private: int tmp; }; A::A(int in){ this->tmp=in; // или же tmp=in; } 
  • 2
    "... a pointer is needed for the most part when we use several objects of the same class ..." is some kind of nonsense. this is the hidden parameter of every non-static class method. On this , all the functionality of classes is fundamentally built. Without it - nowhere, no matter how many objects we have. - AnT
  • association: stackoverflow.com/q/993352/2110496 - vp_arth

3 answers 3

No no need. Or rather, not necessarily. This would be necessary if the parameter were named the same as a member of the class:

 class A{ public: A(int tmp); private: int tmp; } A::A(int tmp){ this->tmp = tmp; } 
  • In the rest of the class functions, is the this pointer necessary? Or also optional? - Kayrosik
  • one
    @John only needed where name conflicts are possible - Igor
  • In this case, you need to use initialization, not assignment, then this not needed: A::A(int tmp):tmp(tmp){} . Well, there is no name conflict in your example, there is a cover-up. - αλεχολυτ

This is more a matter of convention code. this must be used if the name hides another parameter.

Well, or when working with templates:

 template<class T> struct A { int i; }; template<class T> struct B : A<T> { int foo() { return this->i; } }; int main() { B<int> b; b.foo(); } 

Without this-> do not compile, because You must tell the compiler explicitly that i from A<T> .

  • Switched from java? ;) - αλεχολυτ
  • using A<T>::i; - vp_arth
  • @alexolut I started with C ++ at the time. Fortunately, I switched to Java (: - Suvitruf
  • @vp_arth or so, yes. - Suvitruf

In addition to what has already been said in other answers, the presence of this-> immediately tells the reader of the code that we are talking about a member of the class, and not about some other global / local variable. A similar problem, although it is better to solve by code convention, for example, begin all member names with the prefix m or add the suffix in the form of an underscore _ . But even this is not necessary if you work exclusively in modern IDEs with a sufficient level of code analysis - class members will have a separate color / style selection from other entities.

  • And I know, I love to write this->. For example, I prefer this-> swap (other) or std :: swap (this-> m_number, other-> m_number) than the same without this. Of course, using this in such situations absolutely does not affect anything, but without it, it seems as if the “stumps” are obtained. In short, this is who has what handwriting - Jenssen
  • 2
    @Jenssen Yes, my answer is mainly about the style of the code, rather than about the rules of the language itself. Well, even after this-> auto-substitution may work in some IDEs, which is also convenient. - αλεχολυτ