【问题标题】:How can I use friend classes with inheritance and templates如何使用带有继承和模板的朋友类
【发布时间】:2013-12-11 09:51:11
【问题描述】:

我有一个特殊的配置要构建,我不知道怎么写:

template <typename VarType>
class A
{
  protected:
    VarType m_myVar;
}

template <typename VarType>
class B : public A<VarType>
{
}

class C : public B<SpecialType>
{
  void DoSomething()
  {
    m_myVar.PrivateFunction();
  }
}

class SpecialType
{
  private:
    void PrivateFunction()
    {
      //Do something
    }
} 

我怎样才能使用关键字friend使它起作用??

感谢您的回答。

【问题讨论】:

  • 在你的类声明any之后没有分号。祝你编译好运 ;-)
  • 我为不得不维持这种可憎性的人感到抱歉:(

标签: c++ templates inheritance friend


【解决方案1】:

只需声明CSpecialType 的朋友...

class SpecialType
{
  private:
    friend class C;
    void PrivateFunction()
    {
      //Do something
    }
};

【讨论】:

    【解决方案2】:

    在理想的世界中,您也许可以在 SpecialType 类 decl 中写入 friend C::DoSomething();, 但唉,不,你唯一的选择似乎是friend class C;(根据 nyrl)

    不完整类型的朋友和前向声明并没有我们希望的那么好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-30
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 1970-01-01
      相关资源
      最近更新 更多