如何使用成员函数指针来调用另一个类的非静态成员函数 [英] How can I use member-function-pointer to call non-static-member-function of another class

查看:168
本文介绍了如何使用成员函数指针来调用另一个类的非静态成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

有什么办法可以将一个类A的成员函数分配给另一个类B的成员函数指针;然后调用,然后使用类B的member-function-pointer调用类A的成员函数?

Hi all,

Is there any way to assign member-function of a class A to member-function-pointer of another class B; and then call then call that member function of class A using member-function-pointer of class B?

//IN MyParser.h
class CMyParser					//CLASS B
{
public :
	void (*pfnWithinParse)(int  i);
	void Parse(string& sFileName)
	{
		......

		if( m_pfnWithinParse )
			m_pfnWithinParse(0);
		......
	}
};


//IN ExternalUser.h
#include "MyParser.h"

class CExternalUser				//CLASS A
{
public :
	void Use(void)
	{
		CMyParser parser;
		parser.pfWIthinParse = CExternalUser::NonStaticFn;
		parser.Parse();
	}

	void NonStaticFn(int passed)	{}
};



我想在B类成员函数中调用A类的非静态函数NonStaticFn();我该怎么办?

请让我知道是否存在针对上述情况的解决方案,而没有C ++中的抽象类(/interface)或Degelate吗?

谢谢&问候,
Aniket



I want to call non-static function NonStaticFn() of class A in class B member function; How shall I do it?

Please let me know if any solution exist for above situation, without abstract class (/interface) or degelate in C++?

Thanks & Regards,
Aniket

推荐答案

如果要调用类的非静态函数(即方法),则需要一个实例
If you want to call a non-static function (that is a method) of a class, then you need an instance of that class.


除了CPallini所说的那样,您要存储成员函数地址的函数指针类型是错误的-没有相当重量级的强制转换(即-所有演员的木槌),您将永远无法完成任务.即使您这样做了,它也会崩溃并混乱地燃烧.

在一个对象中存储指向对象的指针以及对该对象执行的操作是一个相当普遍的习惯用法.我知道您说过您不想使用委托,但是,这就是您要做的事情:
In addition to what CPallini said the type of function pointer you want to store the address of the member function in is wrong - without a fairly heavyweight cast (i.e. a reinterpret_cast - the mallet of all casts) you''ll never get that assignment to work. And even if you did it''d just crash and burn messily.

It''s a fairly common idiom to store a pointer to an object and the operation to be carried out on the object in another object. I know you said you didn''t want to use a delegate, but, that''s the sort of thing you''re going to have to do:
class B;

class A
{
    public:
        A( B *object, void (B::*function)( int ) )
            : object_( object ), function_( function ){}

        void do_something( int n )
        {
            if( object_ && function_ ) (object_->*function_)( n );
        }

    private:
        void (B::*function_)( int );
        B *object_;
};

class B
{
    public:
        void do_something( int n )
        {
            std::cout << n << std::endl;
        }
};

int main()
try
{
    B b;
    A a( &b, &B::do_something );

    a.do_something( 27 );
}
catch( std::exception &e )
{
    std::cout << "Something went wrong: " << e.what() << std::endl;
}
catch(...)
{
    std::cout << "Something went wrong, no idea what!" << std::endl;
}

这为您提供了两个用于自定义A的变化轴.例如,这非常适合将抽象工厂绑定到普通"工厂方法中.

也许,如果您向我们提供了有关您想做的事情的更多信息(即您要解决的问题,而不是您要解决的问题),那么某人可能会建议您做同样的事情在惯用的C ++中.

This gives you two axes of variation for customising A. As an example this works really well for bundling up abstract factories into "ordinary" factory methods.

Perhaps if you gave us a bit more information about what you want to do (i.e. what the problem is you''re trying to solve, not the solution you''re trying) someone might be able to suggest something that does the same thing in idiomatic C++.


嗨Prasad!

让我们使用朋友类的概念.
结交A类到B类的朋友,然后您就可以访问B类的成员函数和数据成员成为A类.

问候
Arjun
Hi Prasad!

Let''s use friend class concept.
Make friend of Class A to Class B, And then u can access member function and data members of Class B into Class A.

Regards
Arjun


这篇关于如何使用成员函数指针来调用另一个类的非静态成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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