سؤال

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'
هل كانت مفيدة؟

المحلول

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();

نصائح أخرى

in a way there is;

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

but it gets a new name..

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top