Is there a magic keyword that can allow me to ‘implicitly’ access all base class members?

106 Views Asked by At

I'm using a derived class from a base class using templates. I have something like:

#include <iostream>

using namespace std;

template <typename T>
class BaseClass
{
    public:
        BaseClass(T e, T f, T g, T h)
        {
            a = e, b = f, c = g, d = h;
        }

    protected:
        T a, b, c, d;
};

template <typename T>
class DerivedClass : public BaseClass<T>
{
    public:
        //My question is about THIS specific line
        //using BaseClass<T>::a, BaseClass<T>::b, BaseClass<T>::c, BaseClass<T>::d;
        
        //Access and modify base class members once the constructor was called
        DerivedClass(T e, T f, T g, T h) : DerivedClass::BaseClass(e, f, g, h)
        {
            a += e, b += f, c += g, d += h;
        }

        //Access base class members and print them
        void Method2()
        {
            cout << a << endl;
            cout << b << endl;
            cout << c << endl;
            cout << d << endl;
        }

    protected:
      //derived class members
};

int main()
{
    DerivedClass<int> dc(1, 2, 3, 4);
    dc.Method2();
}

I'd like to access the base class members in the derived class. The code works well if I include the commented line (output 2 4 6 8). Otherwise, the compiler can't guess what the members are that I refer to in the derived class.

As far as I could retrieve data from the web, there are two basic ways for telling the compiler to include a base class member in a derived class:

  • one uses this-> before the base member. But I'm too lazy for writing this-> every time I need to.

  • The other one consists of writing using for every member I want to access, which I implemented. But I'm also too lazy for writing that magic word for every base class member.

Is there a way to tell the compiler: "Dude, just point to the base class members every time I write their respective names" with one single magic keyword?

To understand better, I replaced the commented line with using BaseClass<T> but then I get an error:

error: expected nested-name-specifier

So, how can I do it?

0

There are 0 best solutions below