Question

I've got a class which exposes a protected member function of a base class. Is there a way to get a function pointer to the exposed function?

class B
{
protected:
  void foo() {}
};

class D : protected B
{
public:
  using B::foo;
};


void(D::*test)() = &D::foo; // error C2248: 'B::foo' : cannot access protected member declared in class 'D'
Was it helpful?

Solution

It's a bit awkward, but if you can't change the original classes, you could make a derived class to give you access:

struct E : D {
  static void (D::*fooPtr())() { return &D::foo; }
};

void(D::*test)() = E::fooPtr();

OTHER TIPS

in a way there is;

void foo_exposed() { foo(); } // in 'D'

but it gets a new name..

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