c++ call class's function of another class's function
i hope this makes sense for you, i get confused. let me know if there is a simpler way:
double A::a(double(b::*bb)())开发者_C百科
{
b.init();
return (b->*bb)();
}
void A::run();
{
cout<< a(b.b1);
cout<< a(b.b2);
}
class A
{
B b;
void run();
double a(double(b::*bb)());
};
class B
{
void init();
double b1();
double b2();
};
It doesn't make sense. This makes sense though:
class B // <- class B definition comes first
{
void init();
double b1();
double b2();
};
class A
{
B b;
void run();
double a(double(B::*bb)()); // <- B instead of b
};
double A::a(double(B::*bb)()) // <- B instead of b
{
b.init();
return (b->*bb)();
}
void A::run() // <- you can't put semicolon here
{
cout<< a(&B::b1); // <- you want to pass the address of a member.
cout<< a(&B::b2); // <- you want to pass the address of a member.
}
Now it makes more sense to me.
This:
double a(double(b::*bb)());
Should be:
double a(double(B::*bb)());
That is, bb
is to be declared as a pointer to member function in the class B
, not in the object b
(which is an instance, not a type itself, therefore cannot be part of a type).
精彩评论