0

I need some help on a strange mix between function pointers and templates...

My target :

You have a class : template<typename B> class A, and A instanciate a B member. Now I want to acces B getter/setter.

I tried this :

class B_example
{
public:
    B_example(int v):m_var(v){}
    int getVar() { return m_var; }
    void setVar(int v) { m_var = v; }

private:
    int m_var;
};


template<typename B> class A
{
public:
    A():m_b(B(5))
    {
        get = &m_b.getVar;
        set = &m_b.setVar;
    }

    int (B::*get)();
    void (B::*set)(int);

private:
    B m_b;
};


int main(int argc, char** argv)
{
    A<B_example> A_instance;
    B_example B_instance(5);


    int a = (A_instance.get*)();

    std::cout << a << std::endl;
}

Thank's for any help.

Alexandre

8
  • 1
    why not a generalized callback using std::function? Commented Jun 27, 2014 at 13:15
  • Because sometimes I need void(int), sometimes int(void), sometime float(int), sometime Cv::Size(void). I don'know function profile... Commented Jun 27, 2014 at 13:50
  • here's a std::function rewrite, but what you're trying to do is not very clear. If you're just trying to access the fields, do that. Apart from that, I'd recommend you the SOLID principles Commented Jun 27, 2014 at 13:52
  • "Because sometimes I need void(int), sometimes int(void), sometime float(int), sometime Cv::Size(void). I don'know function profile.." that would take some metaprogramming and it still doesn't explain what you're actually trying to do. Can you think of a concise useful example? Commented Jun 27, 2014 at 14:01
  • link TagDetector instantiate IROIDetector, and n times ITag. But I need from the outside of library to acces to every parameters of LineFitting or EdgeDetector or ... (derived from IROIDetector), and to every parameters of HammingTag or StandardTag or LocalThreshTag ... (derived from ITag) Commented Jun 27, 2014 at 14:06

2 Answers 2

1

First, fix the syntax errors:

get = &B::getVar;
set = &B::setVar;

Then, the member-function pointer needs to be called on an object. Without knowing the purpose of these strange pointers, I can't guess what you want to do here. Maybe you want to call on B_instance:

int a = (B_instance.*A_instance.get)();

Or maybe you want to call it on the m_b object within A_instance; but you can't do that because it's private. If that's the case, you probably just want regular member functions, rather than weird function pointers

int get() {return m_b.getVar();}
void set(int v) {m_b.setVar(v);}
Sign up to request clarification or add additional context in comments.

2 Comments

If m_b were public, then A_instance.m_b.*A_instance.get would work. Makes me feel sorry for C++ parser writers :)
My problem is that B is an implementation with 5 parameter, of an interface IB, but I have C where is an implementation with 7 parameters, of IB, and so on... I want to let user acces to this parameter, but B have to stay private in A. So i want to store tuple with : parameter name, get function, set function, so user can easy loop through list of tuple, and get/set parameters I allowed.
0

These:

get = &m_b.getVar;
set = &m_b.setVar;

Should be:

get = &B::getVar;
set = &B::setVar;

And (A_instance.get*)() should be (B_instance.*A_instance.get)().

Comments

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.