简体   繁体   English

我可以参考模板的基础 class 吗?

[英]Can I refer to the template's base class?

Is there a way to refer to the base class of the currently provided template?有没有办法参考当前提供的模板的基础 class ? For example,例如,

template <class UserType>
class User
{
    public:
        User()
        {
            user_manager_ = std::make_unique<UserType>();
        }

   private:
        std::unique_ptr<UserType::Base> user_manager_; // <-- I want this ptr to be the type of base of UserType i.e. IUserManager
}

Nope, because of multiple inheritances in c++.不,因为 c++ 中的多重继承。 The only way to do something similar is:做类似事情的唯一方法是:

struct A {};

struct UserType : public A {
  using Base = A;
};

template<class UserType>
class User {
 public:
  User() {
    user_manager_ = std::make_unique<UserType>();
  }

 private:
  std::unique_ptr<typename UserType::Base> user_manager_; // <-- I want this ptr to be the type of base of UserType i.e. IUserManager
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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