Question

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.

Was it helpful?

Solution

Just declare C as friend of SpecialType...

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

OTHER TIPS

In an ideal world, you could perhaps write friend C::DoSomething(); inside the SpecialType class decl, but alas no, your only option seems to be friend class C; (as per nyrl)

friends and forward declarations of incomplete types doesn't play as nicely as we might hope.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top