Home > Net >  Access every member of base template class
Access every member of base template class

Time:10-22

When you use template inheritence, you have to explicitly specify what members of base template class you intend to use:

template <typename T>
class base 
{
protected:
    int x;
};

template <typename T>
class derived : public base<T> 
{
public:
    int f() { return x; }
protected:
    using base<T>::x;
};

What if base template has a lot of members? Can I avoid writing using declaration for each member and specify that I want every member of base template?

CodePudding user response:

No. There's no such mechanism in the language.

CodePudding user response:

For that reason, i usually introduce a member named inherited.

e.g.

using inherited = base<T>;

Then to access the members of the base class, the code would look like:

int f() { return inherited::x; }

This approach has the advantage, that you can easily differentiate which member you are referring to (if required, using this only for own members).

  •  Tags:  
  • c
  • Related