简体   繁体   中英

C++ variadic templates to cascade inheritance. how to upcast?

Given a cascaded inheritance hierarchy in C++:

struct MyBaseClass
{
protected:
   void f();
};

template <typename Fn, typename... Args>
struct MyClass<Fn, Args...> : MyClass<Args...>
{
...//from here can I access MyBaseClass::f() ?
}; 
template <typename Fn>
struct MyClass<Fn> : MyBaseClass{...};

As indicated in the above comment line, I want to call a protected method from MyBaseClass from the scope of MyClass. How is that possible ?

Maxim的另一个替代解决方案是使用类的名称( demo )完全限定函数名称:

MyBaseClass::f();

I want to call a protected method from MyBaseClass from the scope of MyClass.

There are a few ways you can invoke a template dependent function of a base class from a derived class:

this->f();         // Call f of this class, or any base class.
this->MyClass::f() // Call MyClass::f of MyClass base sub-object of this class only.
MyClass::f()       // Call MyClass::f either of MyClass base sub-object or of any unrelated MyClass.

See Dependent names for more detail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM