Search code examples
c++templatesprotected

Calling a class protected method from a template


I want to have a template that can access the protected method of it's typename parameter. How can I make that work?

For example:

class Foo{
   ...
   protected:
     int Bar();
}

template <class T> FooTempl{
   ...
   int SomeMethod(T* ptr) { return ptr->Bar();};
   ...
}

The reason is that I want the method Foo::Bar() to be accessible to the template, but not to any other external caller. I hope there's some friend syntax there that can make it work...


Solution

  • Add the following line into Foo:

    template<typename T> friend class FooTempl;