由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - a C++ question
相关主题
Test your C++ knowledge...How to check the virtual function table size?
C++ questionoperator overloading (C++)
C++: operator new 为啥要是 static的, 不是有啥影响?access function static variable
inline function是否可以递归?Really Stupid Question: How to run VC++ 2008 Express (转载)
static initialization dependency c++C++ question
why use static function here?问一个面试题
static function and static variable?static void foo( boolean * validPtr ) 中 static 啥意思?
有没有static return type和static as function arguement?"short" in Java
相关话题的讨论汇总
话题: foo话题: void话题: double话题: int话题: static
进入Programming版参与讨论
1 (共1页)
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啊??

1 (共1页)
进入Programming版参与讨论
相关主题
"short" in Javastatic initialization dependency c++
问个INTERVIEW QUESTIONwhy use static function here?
another c++ interview questionstatic function and static variable?
[合集] 为什么 const member 不能是 static.有没有static return type和static as function arguement?
Test your C++ knowledge...How to check the virtual function table size?
C++ questionoperator overloading (C++)
C++: operator new 为啥要是 static的, 不是有啥影响?access function static variable
inline function是否可以递归?Really Stupid Question: How to run VC++ 2008 Express (转载)
相关话题的讨论汇总
话题: foo话题: void话题: double话题: int话题: static