Search code examples
c++templatesinheritancefriend

How can I use friend classes with inheritance and templates


I've got a special configuration to build and I don't know how to write this :

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
    }
} 

How can I use the keyword friend to make it work ??

Thanks for your answers.


Solution

  • Just declare C as friend of SpecialType...

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