成员函数指针困难 [英] Member function pointer woes

查看:59
本文介绍了成员函数指针困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要将指向成员函数的指针作为参数传递给函数

,它将指针指向函数作为一个论点。有什么方法可以这样做吗

除了重载函数吗?


这是我编写的一个小程序,用于检查是否可以获取<的地址br />
函数来自member-function-pointer,这样我就可以把它作为普通函数指针传递给

函数。


------------------------------------------------ ---

#include< stdio.h>


class classname {

public:

int add(int a,int b){

printf(" inside add。\ ta =%d,b =%d \ n",a,b);

返回a + b;

}

} obj;

typedef int(classname :: * mfptr)(int, int);

typedef int(* fptr)(int,int);

main(){


mfptr mem_func_ptr;

fptr func_ptr;


mem_func_ptr =& classname :: add;

func_ptr =(fptr)(& classname: :add);


printf("& classname :: add is%llx \ n",& classname :: add);

printf("& classname :: add is%llx \ n",& classname :: add);


printf(" mem_func_ptr是%llx \ n",mem_func_ptr);

printf(" func_ptr是%x \ n \ n",func_ptr);


printf(使用func_ptr添加调用。 sum是%d \ n \ n",func_ptr(2,3));

printf(" add with mem_func_fptr。sum is%d\\\
\ n",

(obj。* mem_func_ptr)(2,3));

}


--------- --------------------------------------------

我用-Wno-pmf-conversions选项编译它。这是节目的输出




---------------------- --------------------

& classname :: add is ffbeee78ff23e5d0

& classname :: add是ffbeee78ff243a4c

mem_func_ptr是ffbeee78ff243a4c

func_ptr是109bc


里面加。 a = 3,b = 68028

使用func_ptr调用add。总和是68031

里面加。 a = 2,b = 3

使用mem_func_fptr调用add。总和是5


----------------------------------- -------

几个问题:


1.为什么''& classname :: add''的值在两行不同?

2.当''classname :: add''用''func_ptr'调用时,为什么参数''b''的值会改变?
'?

3.如果这种方法不正确,那么请您告知

如何使用它?


谢谢。


Albert。

解决方案



"阿尔伯特QUOT; <人**** @ iitg.ernet.in>在消息中写道

新闻:e9 ************************* @ posting.google.co m ... < blockquote class =post_quotes>

我需要将指向成员函数的指针作为参数传递给函数
,它将指针作为参数。除了重载函数之外,还有什么办法吗?


No.


[snip]

3.如果这种方法不正确,那么请你建议如何使用它?




写一个普通函数调用你的成员函数,然后传递

正常功能作为参数。


john


Albert写道:



我需要将指向成员函数的指针作为参数传递给
函数,该函数将指针作为参数。除了重载功能之外还有其它方法吗?

< snipped>

3.如果这种方法不正确,那么请您指教一下如何使用它?




我建议你考虑使用Boost.Function库。
http://www.boost.org/doc/html/function.html


我用它来创建一个库函数,它可以在不同的程序中使用指针到函数

或指向成员函数的指针。


Mark


" Albert" <人**** @ iitg.ernet.in> skrev i meddelandet

新闻:e9 ************************* @ posting.google.co m ... < blockquote class =post_quotes>我需要将指向成员函数的指针作为参数传递给函数
,它将指针作为参数。有什么方法可以这样做吗
除了重载函数?



你可以使用模板版的订阅者/发布者模式。

的实现并不漂亮,但使用方式有点整洁恕我直言;-)请注意

SubscriberBase和Subscriber可以重复用于其他的通知/>
相同的类型(对于任何类),在这种情况下,成员函数采用int

并返回void。


// Base订阅者类。必须是非模板,因为我们需要存储指向实例的指针。

class SubscriberBase

{

public:

virtual void exec(int)= 0;

};


//实际的订阅者类模板。存储指向对象的指针和

//指向其中一个成员函数。

template< class T> class Subscriber:public SubscriberBase

{

private:

typedef void(T :: * EventFunc)(int);

T *目标; //指向收到通知对象的指针

EventFunc func; //指向要调用的成员函数的指针

public:

订阅者(T * target,EventFunc func):func(func),target(target){}

virtual void exec(int param){(target-> * func)(param); }

};


//这就是它的用法。

//做东西的类通知另一个对象有关某事......

类发布者

{

受保护:

SubscriberBase * sub; //可能真的是一个清单

public:

void registerSubscriber(SubscriberBase& s)

{

sub =& s;

};

void doSomething(int param)

{

// Do一些东西

// ...

sub-> exec(param); // ...并通知

}

};


//测试类。收到通知。

class UserClass

{

public:

//启动整个事情
void start()

{

//需要告诉UserClass这个实例的Publisher对象

某事

发布商测试;


//构建并注册订阅者对象。

订阅者< UserClass> sub(this,callback);

test.registerSubscriber(sub);


//调用发回通知的函数

test.doSomething(12345);

}


//回调成员函数

void callback(int param)

{

cout<< 你好, << param<<结束;

}

};


Hi,

I need to pass a pointer-to-member-function as a parameter to a function
which takes pointer-to-function as an argument. Is there any way to do it
besides overloading the function?

Here is a small program I wrote to check if I could get the address of the
function from the member-function-pointer, so that I could pass it to the
function as a normal function-pointer.

---------------------------------------------------
#include<stdio.h>

class classname{
public:
int add(int a, int b){
printf("inside add. \ta = %d, b = %d\n", a,b);
return a+b;
}
}obj;
typedef int(classname::*mfptr)(int, int);
typedef int (*fptr)(int, int);
main(){

mfptr mem_func_ptr;
fptr func_ptr;

mem_func_ptr = &classname::add;
func_ptr = (fptr)(&classname::add);

printf("&classname::add is %llx\n", &classname::add);
printf("&classname::add is %llx\n", &classname::add);

printf("mem_func_ptr is %llx\n", mem_func_ptr);
printf("func_ptr is %x\n\n", func_ptr);

printf("add called with func_ptr. sum is %d\n\n", func_ptr(2,3));
printf("add called with mem_func_fptr. sum is %d\n\n",
(obj.*mem_func_ptr)(2,3));
}

-----------------------------------------------------
I compiled it with the -Wno-pmf-conversions option. Here is the output of
the program:

------------------------------------------
&classname::add is ffbeee78ff23e5d0
&classname::add is ffbeee78ff243a4c

mem_func_ptr is ffbeee78ff243a4c
func_ptr is 109bc

inside add. a = 3, b = 68028
add called with func_ptr. sum is 68031

inside add. a = 2, b = 3
add called with mem_func_fptr. sum is 5

------------------------------------------
A few questions:

1. Why is the value of ''&classname::add'' in the two lines different?
2. Why does the value of parameter ''b'' change when ''classname::add'' is
called with ''func_ptr''?
3. If this approach is not correct, then could you please advise as to how
to go about with it?

Thanks.

Albert.

解决方案


"Albert" <al****@iitg.ernet.in> wrote in message
news:e9*************************@posting.google.co m...

Hi,

I need to pass a pointer-to-member-function as a parameter to a function
which takes pointer-to-function as an argument. Is there any way to do it
besides overloading the function?
No.

[snip]
3. If this approach is not correct, then could you please advise as to how
to go about with it?



Write a normal function which calls your member function and then pass the
normal function as a parameter.

john


Albert wrote:

Hi,

I need to pass a pointer-to-member-function as a parameter to a
function which takes pointer-to-function as an argument. Is there any
way to do it besides overloading the function?

<snipped>

3. If this approach is not correct, then could you please advise as
to how to go about with it?



I would advise you to consider using the Boost.Function library.
http://www.boost.org/doc/html/function.html

I have used it to have a library function which can take pointer-to-function
or pointer-to-member-function in different programs.

Mark


"Albert" <al****@iitg.ernet.in> skrev i meddelandet
news:e9*************************@posting.google.co m...

I need to pass a pointer-to-member-function as a parameter to a function
which takes pointer-to-function as an argument. Is there any way to do it
besides overloading the function?


You could use a templated version of the subscriber/publisher pattern. The
implementation isn''t pretty, but the usage is kind of neat IMHO;-) Note that
SubscriberBase and Subscriber can be reused for other notifications of the
same type (for any class), in this case member functions that take an int
and return void.

// Base class for Subscriber. Must be a non-template since we
// need to store a pointer to an instance.
class SubscriberBase
{
public:
virtual void exec(int) = 0;
};

// The actual Subscriber class template. Stores a pointer to an object and a
pointer
// to one of it''s member functions.
template<class T> class Subscriber : public SubscriberBase
{
private:
typedef void (T::*EventFunc)(int);
T *target; // Pointer to the object to recieve notification
EventFunc func; // Pointer to member function to be called
public:
Subscriber(T *target, EventFunc func) : func(func), target(target) { }
virtual void exec(int param) { (target->*func)(param); }
};

// And this is how it''s used.
// Class that does stuff and notifies another object about something...
class Publisher
{
protected:
SubscriberBase *sub; // Could be a list really
public:
void registerSubscriber(SubscriberBase &s)
{
sub = &s;
};
void doSomething(int param)
{
// Do something
// ...
sub->exec(param); // ... and notify
}
};

// Test class. Recieves notifications.
class UserClass
{
public:
// Starts the the entire thing
void start()
{
// Publisher object that needs to tell this instance of UserClass about
something
Publisher test;

// Construct and register a subscriber object.
Subscriber<UserClass> sub(this, callback);
test.registerSubscriber(sub);

// Call a function that sends back notifications
test.doSomething(12345);
}

// Callback member function
void callback(int param)
{
cout << "Hello, " << param << endl;
}
};


这篇关于成员函数指针困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆