如何使用具有继承和模板的友元类

How can I use friend classes with inheritance and templates

本文关键字:友元 何使用 继承      更新时间:2023-10-16

我有一个特殊的配置要构建,我不知道如何编写:

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使其工作??

谢谢你的回答。

只需将C声明为SpecialType的朋友。。。

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

在理想的情况下,您可以在SpecialType类decl中编写friend C::DoSomething();,但遗憾的是,你唯一的选择似乎是friend class C;(根据nyrl)

不完全类型的friends和forward声明并不像我们希望的那样好。