0

Is there a way to get function pointer for a member function that is private inside a class

class A
{
public:
    void callMe()
    {
        cout<<__FUNCTION__<<endl;
    }

private:
    void fooMem()
    {
        cout<<__FUNCTION__<<endl;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    auto fp = &A::fooMem;

    return 0;
}

Compiling this in vs 2012 c++ compiler causes below error

error C2248: 'A::fooMem' : cannot access private member declared in class 'A'
see declaration of 'A::fooMem'

I looked into a amazing solution to a similar problem (though I am not very clear how this actually works, if someone can explain that would be great too), Here I want the address of the member not to invoke it.

The reason I am asking for the address is I'll be patching this function with a different implementation.

The class as such is not modifiable, But I can inherit if that can help achieve this.

Community
  • 1
  • 1
user3279954
  • 556
  • 2
  • 7
  • 22
  • First, a pointer to member function is not simply an address; it has to support virtual calls (even if the member function it points to isn't virtual), so its structure is more complicated. And second, no, if the function is private it's not accessible from outside the class. – Pete Becker Jul 12 '16 at 02:47
  • If you follow the link I referenced above, the code compiles and successfully invokes the private member. Since I don't completely follow the implementation I couldn't reuse it for my need. Also this is for test code (unit testing, production code), so tampering private members is fine :) – user3279954 Jul 12 '16 at 02:49
  • 2
    Shrug. "it compiles" is not the same as "It works because it does X and the language definition says what X means". – Pete Becker Jul 12 '16 at 02:51
  • The question you linked has a comment indicating that the trick doesn't work with the VC++ compiler. That said, I don't understand the downvote. Compare to Herb Sutter's [Uses and Abuses of Access Rights](http://www.gotw.ca/gotw/076.htm). – dxiv Jul 12 '16 at 03:04

1 Answers1

-1

Since you cannot modify the original class, it would be much easier to simply inherit the class and create a duplicate of the function in a public memberspace.

I tried this out and it works as expected, at least with g++:

#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

class A
{
    public:
        void aFunction()
        {
            cout<<"This is aFunction"<<endl;
        }
    private:
        void anotherFunction()
        {
            cout<<"This is anotherFunction"<<endl;
        }
};

class B: public A
{
    public:
        void anotherFunction()
        {
            cout<<"This is also anotherFunction, but it's accessible!"<<endl;
        }
};

int main(int argc, char* argv[])
{
    A firstClass;
    B coach;
    firstClass.aFunction();
    coach.anotherFunction();
    return 0;
}

When I run this code I get the following output:

$ ./a.out
This is aFunction
This is also anotherFunction, but it's accessible!

Proving the compiler understood which version of anotherFunction to use.

Josh Benson
  • 236
  • 3
  • 12
  • As I mentioned I dont need to call the private implementation. Actual scenario is something like this---- The non virtual anotherFunction is called inside the aFunction. I need to stub the anotherFunction with my external code. – user3279954 Jul 12 '16 at 07:51