从B打电话给A. [英] Call A from B

查看:50
本文介绍了从B打电话给A.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于A级和B级:


A级

{

公开:

void method_for_B_to_call();

....

};


如何让B回调method_for_B() A而不让B知道A的定义(例如不使用#includeA.h),但只知道

A :: method_for_B_to_call( )回电?


谢谢!

解决方案



Ele < El*@inter.com>在消息中写道

新闻:Ii ******************** @ bgtnsc05-news.ops.worldnet.att.net ... < blockquote class =post_quotes>鉴于A级和B级:

A级
{
公开:
void method_for_B_to_call();
.. 。
};

如何让B回调A的method_for_B()而不让B知道A的定义(例如不使用#includeA .h"),但只知道A :: method_for_B_to_call()回电?

谢谢!




我不喜欢除非method_for_B_to_call是静态的

方法,否则我认为这是可能的。是吗?


john


Ele写道:


鉴于A级和B级:

A级
公开:
void method_for_B_to_call();
...
};

如何让B回调A的method_for_B()而不让B知道A的定义(例如不使用#includeA.h),但只知道
A :: method_for_B_to_call()用于回拨?

谢谢!



您无法直接在定义中执行此操作。你需要为B类找到一个

抽象来使用;确切地说 - 取决于细节

您的问题。


这里有两种方法:


0)代表团:


//////标题(由B类使用):

A类; //前方声明


//由B级使用:

C级{

A * pa_;


public:

void method_for_B_to_call();

};

//////.cpp文件:包括上面的标题和A类的定义

void C :: method_for_B_to_call(){

return pa _-> method_for_B_to_call();

}

1)抽象基类(接口)A:


//由B使用,由A实现
class ABase {

public:

virtual~ABase()= 0;

virtual void method_for_B_to_call()= 0;

// ...

};

Denis


2004年5月26日星期三05 :格林威治标准时间54:48,Ele < El*@inter.com>写道:

鉴于A级和B级:

A级
公开:
void method_for_B_to_call() ;
...
};

如何让B回调A的method_for_B()而不让B知道A的定义(例如不使用" #include" A.h"),但只知道A :: method_for_B_to_call()用于回拨?


我似乎已经使用指向成员的工作了,尽管它是
凌晨3点而且我从来没有得过指针 - 成员工作

之前;-)


试试这个(我不认为宣布A是B'的好处的不完整类型

有资格作为让B知道A的定义。至少我希望它

不......):

// bh:

A级;


B级

{

公开:

typedef void(A :: * ftype)();

void CallIt(A * ap,ftype f);

};

// b.cpp:


#include" b.h"


void B :: CallIt( A * ap,ftype f)

{

(ap-> * f)();

}

// amain.cpp:


#include< iostream>

使用std :: cout;

使用std :: endl;


#include" b.h"


A级

{

public:

void method_for_B(){

std :: cout<< In method_for_B ...() << std :: endl;

}


};


int main()

{

void(A :: * fp)()= A :: method_for_B;

A a1;

B b1;

b1.CallIt(& a1,fp);

返回0;

}


输出:

方法_for_B ...()


-leor

谢谢!




-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD Software的免费STL错误消息解密器:
www.bdsoft.com/tools/stlfilt.html


Given class A and class B:

class A
{
public:
void method_for_B_to_call();
....
};

How to make B to call back method_for_B() of A without letting B know the
definition of A (e.g. not using "#include "A.h"), but only know
A::method_for_B_to_call() for calling back?

Thanks!

解决方案


"Ele" <El*@inter.com> wrote in message
news:Ii********************@bgtnsc05-news.ops.worldnet.att.net...

Given class A and class B:

class A
{
public:
void method_for_B_to_call();
...
};

How to make B to call back method_for_B() of A without letting B know the
definition of A (e.g. not using "#include "A.h"), but only know
A::method_for_B_to_call() for calling back?

Thanks!



I don''t think that is possible unless method_for_B_to_call is a static
method. Is it?

john


Ele wrote:


Given class A and class B:

class A
{
public:
void method_for_B_to_call();
...
};

How to make B to call back method_for_B() of A without letting B know the
definition of A (e.g. not using "#include "A.h"), but only know
A::method_for_B_to_call() for calling back?

Thanks!


You cannot do it directly in your definitions. You need to find an
abstraction for class B to use; which exactly - depends on the details
of your problem.

Here are two approaches:

0) Delegation:

//////header (used by class B):
class A; //forward declaration

//used by class B:
class C {
A* pa_;

public:
void method_for_B_to_call();
};
//////.cpp file: includes the above header and the definition of class A
void C::method_for_B_to_call() {
return pa_->method_for_B_to_call();
}
1) Abstract base class (interface) for A:

//used by B, implemented by A
class ABase {
public:
virtual ~ABase() = 0;
virtual void method_for_B_to_call() = 0;
//...
};
Denis


On Wed, 26 May 2004 05:54:48 GMT, "Ele" <El*@inter.com> wrote:

Given class A and class B:

class A
{
public:
void method_for_B_to_call();
...
};

How to make B to call back method_for_B() of A without letting B know the
definition of A (e.g. not using "#include "A.h"), but only know
A::method_for_B_to_call() for calling back?
I seem to have gotten it to work using a pointer-to-member, despite it
being 3am and my never having ever gotten a pointer-to-member to work
before ;-)

Try this (I don''t think declaring A as an incomplete type for B''s benefit
would qualify as "letting B know the definition of A". At least I hope it
doesn''t...):
// b.h:
class A;

class B
{
public:
typedef void (A::*ftype)();
void CallIt(A *ap, ftype f);
};
// b.cpp:

#include "b.h"

void B::CallIt(A *ap, ftype f)
{
(ap->*f)();
}
// amain.cpp:

#include <iostream>
using std::cout;
using std::endl;

#include "b.h"

class A
{
public:
void method_for_B() {
std::cout << "In method_for_B...()" << std::endl;
}

};

int main()
{
void (A::*fp)() = A::method_for_B;
A a1;
B b1;
b1.CallIt(&a1, fp);
return 0;
}

Output:
In method_for_B...()

-leor


Thanks!



--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software''s free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html


这篇关于从B打电话给A.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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