0

I have written a template base class as below:

template<typename T> class Base { public: Base(T x); void print_base(); virtual ~Base(); protected: T data; }; template<typename T> Base<T>::Base(T x) : data(x) {} template<typename T> void Base<T>::print_base() { std::cout << data << std::endl; } template<typename T> Base<T>::~Base() {} 

Now, I am trying to write a derived class as the below:

template<typename T> class Derived : public Base<T> { public: Derived(T x); void print_derived(); private: }; template<typename T> Derived<T>::Derived(T x) : this->Base(x) {} template<typename T> void Derived<T>::print_derived() { this->print_base(); std::cout << " data " << this->data << std::endl; } 

But the compiler reports error on the below line:

template<typename T> Derived<T>::Derived(T x) : this->Base(x) {} 

The error is as below:

template_derived_class_this.h:12:28: error: expected identifier before 'this' Derived<T>::Derived(T x) : this->Base(x) {} 

If I change it to the below the error goes away:

 Derived<T>::Derived(T x) : Base<T>::Base(x) {} 

My question is what is the reason for the error in the this-> prefix format? Is it because before the constructor has finished running the this pointer is not yet ready or am I making any syntactical error here? I have seen this-> being used inside the constructor elsewhere. So not sure what is the reason for the error in my case. I am compiling with the below command:

g++ template_inheritance_main_this.cpp -o template_inheritance_main_this -Wall -Wextra -Wpedantic -Werror 
1
  • 1
    use template<typename T>Derived<T>::Derived(T x) : Base<T>(x) {} instead. Commented May 20, 2021 at 11:40

1 Answer 1

2

You can use this inside constructor body, during initialisation of base class this is unavailable and you just use base class name to initialise it as you did in changed code snippet of yours. Constructor of your derived class should look like this:

template<typename T> Derived<T>::Derived(T x) : Base<T>(x) {} 
Sign up to request clarification or add additional context in comments.

4 Comments

If I use the code snippet you have specified I get an error the compiler saying class 'Derived<T>' does not have any field named 'Base' as the Base<T> is a dependant class. The compiler doesn't consider the dependant class symbols for name resolution. This post talks about it stackoverflow.com/questions/50321788/….
It looks like it might be compiler specific and MSVC allows such solution. Most probably using Base<T> will resolve that problem in gcc.
Unfortunately, g++ is not liking the Base<T> (without the scope resolution operator) either. I just gave it a try and the compiler says template_derived_class_this.h:12:35: error: expected '(' before 'Base' Derived<T>::Derived(T x) : Base<T>Base(x) {. But my emphasis was more on why we cannot use the this-> prefix. Looks like it is because how the this pointer is initialised at the time of object creation.
I meant just Base<T> not Base<T>Base. Regarding this as I mentioned in my answer, it's unavailable during initialization and you can use it only inside constructor body (and in other methods bodies).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.