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
thecdoctor
  • 69
  • 14

1 Answers1

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) {}
ostojan
  • 608
  • 1
  • 7
  • 17
  • If I use the code snippet you have specified I get an error the compiler saying `class 'Derived' does not have any field named 'Base'` as the Base is a dependant class. The compiler doesn't consider the dependant class symbols for name resolution. This post talks about it https://stackoverflow.com/questions/50321788/a-better-way-to-avoid-public-member-invisibility-and-source-code-bloat-repetitio. – thecdoctor May 20 '21 at 11:35
  • It looks like it might be compiler specific and MSVC allows such solution. Most probably using `Base` will resolve that problem in gcc. – ostojan May 20 '21 at 11:46
  • Unfortunately, g++ is not liking the Base (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::Derived(T x) : BaseBase(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. – thecdoctor May 20 '21 at 12:04
  • 1
    I meant just `Base` not `BaseBase`. 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). – ostojan May 20 '21 at 12:21