Open In App

Why Do I Have to Access Template Base Class Members Through the ‘this’ Pointer?

Last Updated : 22 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, when we have a class template that inherits from a template base class, we need to explicitly use this pointer to access members of the base class. In this article, we will learn why it is necessary to access template base class members through ‘this’ pointer in C++.

The Problem in Accessing Template Base Class Members Without this Pointer

When a template class derives from another template class, the base class’s members are considered dependent on the template parameters and are unknown to the compiler until the template is instantiated. This creates a problem where the derived class template does not automatically consider the base class scope when compiling the template. As a result, if we try to use a base class member directly in the derived class without qualification, the compiler can’t do anything meaningful to fix it in the first stage of compilation.

In the following program, we try to access the template base class members without this pointer to see what are the consequences:

C++
// C++ program to access template base class members without
// 'this' pointer

#include <iostream>
using namespace std;

template <typename T> class Base {
public:
    void display() { cout << "Hello Geeks" << endl; }
};

template <typename T> class Derived : public Base<T> {
public:
    void display2()
    {
        // Accessing base class member without 'this'
        // pointer
        display();
    }
};

int main()
{
    Derived<int> obj1;
  
    // calling a function of the derived class
    obj1.display2();

    return 0;
}


Output

error: there are no arguments to 'display' that depend on a template parameter, so a declaration of 'display' 
must be available [-fpermissive]

Using ‘this' Pointer to Access Template Base Class Members

To resolve the issue of dependent names, we can use the this pointer in the derived class to make sure that the name should be looked up in the context of the base class only. The this pointer is a pointer to the current instance of the class, so using it we can guide the compiler about where to look for the name.

C++ Program to Access Template Base Class Members through ‘this’ Pointer

The below example demonstrates the use of the ‘this’ pointer to access base class members in a class template.

C++
// C++ program to access template base class members through
// the 'this' pointer

#include <iostream>
using namespace std;

template <typename T> class Base {
public:
    void display() { cout << "Hello Geeks" << endl; }
};

template <typename T> class Derived : public Base<T> {
public:
    void display2()
    {
        // Accessing base class member through 'this'
        // pointer
        this->display();
    }
};

int main()
{
    Derived<int> obj1;
    // calling a function of the derived class
    obj1.display2();

    return 0;
}

Output
Hello Geeks

Time Complexity: O(1)
Auxiliary Space: O(1)





Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads