2

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'

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

Comments

0

in a way there is;

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

but it gets a new name..

1 Comment

Doesn't have to: void foo() { B::foo(); }. It is indeed a solution but I would prefer not changing my original code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.