f******y 发帖数: 2971 | 1 void f( void (*aa)(int, double, ...), int, double, ...) //defined in
an outside library, not easy to change
class A
{
private:
......
.......
public:
void foo_1 (int, double, ....);
void foo_2()
{
................
................
f(foo_1, ............);
}
}
The last code cannot compile. It says,
error: in function 'A:: foo_2()'
argument of type ‘voi | P********e 发帖数: 2610 | 2 again?
make it static
void f( void (*aa)(int, double, ...), int, double, ...) //defined in
an outside library, not easy to change
class A
{
private:
......
.......
public:
void foo_1 (int, double, ....);
void foo_2()
{
................
................
f(foo_1, ............);
}
}
The last code cannot compile. It says,
error: in function 'A:: foo_2()
【在 f******y 的大作中提到】 : void f( void (*aa)(int, double, ...), int, double, ...) //defined in : an outside library, not easy to change : class A : { : private: : ...... : ....... : public: : void foo_1 (int, double, ....); : void foo_2()
| f******y 发帖数: 2971 | 3 but i don't have static variables in this class
in
【在 P********e 的大作中提到】 : again? : make it static : : void f( void (*aa)(int, double, ...), int, double, ...) //defined in : an outside library, not easy to change : class A : { : private: : ...... : .......
| h**o 发帖数: 347 | 4 but you cant use a pointer to a member function.
static member function does not necessarily mean there must be static
varibles. it really depends.
you shouldnt write the code like this anyway. that may creat a loop of
function calls
【在 f******y 的大作中提到】 : but i don't have static variables in this class : : in
| p****o 发帖数: 1340 | 5 the most elegant way is to define sth similar to mem_fun. not sure
how to wrap the special parameter "..."
or you can define a static function in A to wrap foo_1, and use
another static member to pass the current this pointer.
class A
{
private:
static void foo_1_wrap(int, double, ...)
{
current_this->foo_1(int, double, ...)
}
static current_this;
public:
foo_2(...)
{
current_this = this;
f(foo_1_wrap, int, double, ...);
【在 f******y 的大作中提到】 : void f( void (*aa)(int, double, ...), int, double, ...) //defined in : an outside library, not easy to change : class A : { : private: : ...... : ....... : public: : void foo_1 (int, double, ....); : void foo_2()
| h**o 发帖数: 347 | 6 is this safe?
should he use tr1::function?
【在 p****o 的大作中提到】 : the most elegant way is to define sth similar to mem_fun. not sure : how to wrap the special parameter "..." : or you can define a static function in A to wrap foo_1, and use : another static member to pass the current this pointer. : class A : { : private: : static void foo_1_wrap(int, double, ...) : { : current_this->foo_1(int, double, ...)
| p****o 发帖数: 1340 | 7 it is of course safe, and dirty fast.
tr1::function is okay -- it is similar to mem_fun (i actually don't
know much about it), but it is too fancy. anyway, we write code to
solve problems.
【在 h**o 的大作中提到】 : is this safe? : should he use tr1::function?
| d*******d 发帖数: 2050 | 8 这样试试,我也不确定是不是真的work,你f的定义要变一下:
class A; //先要declare一下A。
void f(void (A::*aa)(int, double,......), int, double,.....);
in
【在 f******y 的大作中提到】 : void f( void (*aa)(int, double, ...), int, double, ...) //defined in : an outside library, not easy to change : class A : { : private: : ...... : ....... : public: : void foo_1 (int, double, ....); : void foo_2()
| f******y 发帖数: 2971 | 9 void f(....................)没法更改。
【在 d*******d 的大作中提到】 : 这样试试,我也不确定是不是真的work,你f的定义要变一下: : class A; //先要declare一下A。 : void f(void (A::*aa)(int, double,......), int, double,.....); : : in
| f******y 发帖数: 2971 | 10 static void foo_1_wrap(int, double, ...)
{
current_this->foo_1(int, double, ...)
}
这个包装似乎有问题:
****.cc:45: error: expected primary-expression before ‘double’
****.cc:45: error: expected primary-expression before ‘double’
****.cc:45: error: expected primary-expression before ‘int’
****.cc:45: error: expected primary-expression before ‘int’
****.cc:45: error: expected primary-expression before ‘void’
上面出现的就是foo_1,和 foo_1_wrap的参数。
【在 p****o 的大作中提到】 : the most elegant way is to define sth similar to mem_fun. not sure : how to wrap the special parameter "..." : or you can define a static function in A to wrap foo_1, and use : another static member to pass the current this pointer. : class A : { : private: : static void foo_1_wrap(int, double, ...) : { : current_this->foo_1(int, double, ...)
| t****t 发帖数: 6806 | 11 come on,用点脑子想想好不好
你要调用member function,总得给个对象是吧,那对象呢???你写个current_this是啥东
西,编译器大概没这么聪明了解你的意思.
要是没有对象,你用的哪门子member function啊??
【在 f******y 的大作中提到】 : static void foo_1_wrap(int, double, ...) : { : current_this->foo_1(int, double, ...) : } : 这个包装似乎有问题: : ****.cc:45: error: expected primary-expression before ‘double’ : ****.cc:45: error: expected primary-expression before ‘double’ : ****.cc:45: error: expected primary-expression before ‘int’ : ****.cc:45: error: expected primary-expression before ‘int’ : ****.cc:45: error: expected primary-expression before ‘void’
| f******y 发帖数: 2971 | 12 呵呵,不是你说的这个原因,问题已经解决了。下面定义的时候,我写成了
static void foo_1_wrap(int x, double y, ...)
{
current_this->foo_1(int x, double y, ...)
}
要把第二行的 int, double什么的都去掉。也是个愚蠢的错误。
谢谢大加帮助。
【在 t****t 的大作中提到】 : come on,用点脑子想想好不好 : 你要调用member function,总得给个对象是吧,那对象呢???你写个current_this是啥东 : 西,编译器大概没这么聪明了解你的意思. : 要是没有对象,你用的哪门子member function啊??
|
|