N
Newsgroup - Ann
Gurus,
I have the following implementation of a member function:
class A
{
// ...
virtual double func(double v);
void caller(int i, int j, double (* callee)(double));
void foo() {caller(1, 2, func);
}
The compiler told me there's a syntax error, because the pointer to a member
function is different from a pointer to a regular function.
That is, the func cannot be passed to caller. I checked the FAQ and in "[30]
Pointers to member functions", it says the pointers to a member function and
to a regular member function are different. Ok, then I tried to change it to
static, and it works because static member function is like a regular
function. But a static function cannot be virtual. So I have to use
something suggested in the FAQ[30] like this:
void caller(int i, int j, double (A::*callee)(double))
{
// ...
(this->*callee)(param);
}
Isn't this an awkard solution? My question is: why was the C++ designed like
this, couldn't there be a easy and convenient way to pass the pointer like
a regular function? All the functions are in the same class, shouldn't the
so-called encapsulation make it convenient? Or maybe it's due to my
misunderstanding and poor design?
Thanks.
I have the following implementation of a member function:
class A
{
// ...
virtual double func(double v);
void caller(int i, int j, double (* callee)(double));
void foo() {caller(1, 2, func);
}
The compiler told me there's a syntax error, because the pointer to a member
function is different from a pointer to a regular function.
That is, the func cannot be passed to caller. I checked the FAQ and in "[30]
Pointers to member functions", it says the pointers to a member function and
to a regular member function are different. Ok, then I tried to change it to
static, and it works because static member function is like a regular
function. But a static function cannot be virtual. So I have to use
something suggested in the FAQ[30] like this:
void caller(int i, int j, double (A::*callee)(double))
{
// ...
(this->*callee)(param);
}
Isn't this an awkard solution? My question is: why was the C++ designed like
this, couldn't there be a easy and convenient way to pass the pointer like
a regular function? All the functions are in the same class, shouldn't the
so-called encapsulation make it convenient? Or maybe it's due to my
misunderstanding and poor design?
Thanks.