質問

In the example pointers/countingptr.hpp of the book C++ Templates - The Complete Guide members of the derived dependent class CountingPtr are referred to using the this pointer. Why is this necessary in this example?

I'm aware that the this pointer is required in order to name members of a dependent base class template. Surely the this pointer is not required in dependent derived class template also?

役に立ちましたか?

解決

I believe it's just a style of the guy who was making this code. Some people prefer putting this-> in front of anything that is related to a class inside that class. That indeed may sometimes be useful if you are doing funny things like:

void foo( int a )
{
this->a = a;
}

or if you just think this increases readability. However, if you use it too much, it will be a mess:

this->a = this->b * this->c - this->foo( this->d, this->bar() );

他のヒント

It's not needed. Some people use this everywhere.

not required in dependent derived class

In what?

There is no "dependent derived class". The derived class is the derived class.

derived class template

It is a class template, but it is parsed as a class.

Think of it this way:

It is not a class template, just a class (a type). A class template specialisation, but still a class, like a normal class.

Or this way:

The derived class template is instantiated with a "magical lazy type" argument. This lazy type argument suspends the compilation of any expression which depends on it.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top