1

I have 2 classes ClassA and ClassB as bellow. How can I Initialize a classA object inside the constructor of classB that passing pointer to a method of ClassA?

Thanks so much!!

typedef int (*doWork)(int, int);

int GlobalAdd(int a, int b) {
    return a + b;
}

class ClassA {
public:
    ClassA(doWork){}
};

class ClassB 
{
    bool mIsBlocked = false;

public:
    ClassB() {
        ClassA A1(GlobalAdd); // OK
        ClassA A2(&ClassB::MemberAdd); // error C2664: 'ClassA::ClassA(ClassA &&)': cannot convert argument 1 from 'int (__thiscall ClassB::* )(int,int)' to 'doWork'
    }

    int MemberAdd(int a, int b)
    {
        if(mIsBlocked)
            return a + b;
        return -1;
    }
};
Anna
  • 43
  • 5
  • Your answers is not work in my case. Your case is call function member of an object of the class ***foo object* but my case is call member of the class inside its constructor – Anna Oct 13 '18 at 09:21

0 Answers0