473,407 Members | 2,359 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Call A from B

Ele
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!

Jul 22 '05 #1
14 1805

"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
Jul 22 '05 #2
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
Jul 22 '05 #3
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
Jul 22 '05 #4
On Wed, 26 May 2004 07:52:55 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:

"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?


That's what I thought, after writing up the example almost the way I just
posted it, and getting an error when I tried to invoke that function
through the pointer-to-member (the error message said A was undefined).. I
even had a response all written up saying essentially what you said above,
that it could only be done with a static member function (or some
non-member function).

Then inspiration hit: I put an asterisk after the -> in my call ;-)

If I've "cheated" somehow without knowing what I'm doing, I wouldn't be
surprised. But it /looks/ to me like what I wrote fits the job description.
-leor

--
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
Jul 22 '05 #5
Leor Zolman wrote:

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 ;-)


Thanks for the embarrasment. Everytime I say "cannot do it" someone shows
me how to do it.

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;
The above didn't compile for me (said it was non-standard). I had to change
it to
void (A::*fp)() = &A::method_for_B;
A a1;
B b1;
b1.CallIt(&a1, fp);
return 0;
}

Output:
In method_for_B...()

-leor


Denis
Jul 22 '05 #6
Denis Remezov wrote:

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.


Sorry, I was wrong, you can do it directly (see Leor's post).
(I'd still prefer not to use pointers to member functions, but
that's beyond the point).

Denis
Jul 22 '05 #7
"Leor Zolman" <le**@bdsoft.com> wrote in message
news:du********************************@4ax.com...
On Wed, 26 May 2004 05:54:48 GMT, "Ele" <El*@inter.com> wrote: .... 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)();
}


Can be done this way. But I suspect that this is
not guaranteed to work protably by the C++ standard
(calling a member function without having seen the
definition of the class).

If this was an assignment, I think that using a virtual
method call in an abstract base would be the way to go.

A more generic way to do this would be to use a
callback object, such as the one provided by boost:
http://www.boost.org/doc/html/function.html
(IIRC, this mechanism is expected to be adopted
in the next C++ standard).

For an example related to your situation, see:
http://www.boost.org/doc/html/functi...html#id2511537
Once the function object is created, the calling code
does not need to know anything about the called class.
Cheers,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Jul 22 '05 #8
Denis Remezov wrote:

Denis Remezov wrote:

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.


Sorry, I was wrong, you can do it directly (see Leor's post).
(I'd still prefer not to use pointers to member functions, but
that's beyond the point).

Denis


On the second (or is it the third) thought, 5.5.3, when describing the
operator ->* , mentions that the class type "is a completely-defined class
type". Does that mean that calling a function through a pointer-to-member
of an incomplete type is [formally] UB?

Denis
Jul 22 '05 #9
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2h************@uni-berlin.de>...
"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?


Intermediate, e.g. boost::function? Of course, the code which assigns
&A::method_for_B_to_call must still #include "A.h"

Regards,
Michiel Salters
Jul 22 '05 #10
On Wed, 26 May 2004 04:28:58 +0000, Denis Remezov
<RE*********************@yahoo.removethis.ca> wrote:

On the second (or is it the third) thought, 5.5.3, when describing the
operator ->* , mentions that the class type "is a completely-defined class
type". Does that mean that calling a function through a pointer-to-member
of an incomplete type is [formally] UB?
As I said in my response to John, I wasn't sure if I'd somehow managed to
cheat the devil here. It would seem likely I have. But even Comeau accepts
it as written (except for the missing & on taking the address of the
function) ...I've put a query in to Greg and EDG. We'll see what
transpires. But right now it's looking to me a lot like undiagnosed UB,
alas. And I was feeling so proud of myself, too ;-)
-leor

Denis


--
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
Jul 22 '05 #11
On Wed, 26 May 2004 03:33:35 +0000, Denis Remezov
<RE*********************@yahoo.removethis.ca> wrote:
Leor Zolman wrote:

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 ;-)


Thanks for the embarrasment. Everytime I say "cannot do it" someone shows
me how to do it.


Well, I can now share some of the embarrassment with you. John Spicer at
EDG has informed me that EDG believes an error should be issued in strict
mode for that Stupid Incomplete Type Trick I pulled.

I was rather surprised when it worked, but perhaps the fact it was 3am was
enough to keep me from pursuing my suspicions that I may have been "getting
away with something" by looking up pointer-to-member rules in the Standard.

I just realized something rather funny: when I was testing, I don't believe
I was even configured for strict mode. But sure enough, there's no
diagnostic even when I switch to strict mode. This may be as close as I
ever get to uncovering an EDG "bug" ;-)
-leor

--
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
Jul 22 '05 #12
Leor Zolman wrote:

On Wed, 26 May 2004 03:33:35 +0000, Denis Remezov
<RE*********************@yahoo.removethis.ca> wrote:
Leor Zolman wrote:

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 ;-)


Thanks for the embarrasment. Everytime I say "cannot do it" someone shows
me how to do it.


Well, I can now share some of the embarrassment with you. John Spicer at
EDG has informed me that EDG believes an error should be issued in strict
mode for that Stupid Incomplete Type Trick I pulled.

I was rather surprised when it worked, but perhaps the fact it was 3am was
enough to keep me from pursuing my suspicions that I may have been "getting
away with something" by looking up pointer-to-member rules in the Standard.

I just realized something rather funny: when I was testing, I don't believe
I was even configured for strict mode. But sure enough, there's no
diagnostic even when I switch to strict mode. This may be as close as I
ever get to uncovering an EDG "bug" ;-)
-leor


It's interesting to learn that.

At first, I didn't even think about a possibility of calling a member on an
incomplete type.
Then, all of a sudden, neither GCC 3.3.3 nor even Comeau Online complained a
bit about your example (corrected for the '&' typo), ansi/pedantic and strict
enabled. That's quite rare for a feature that is non-standard and not (I assume)
a planned extension due to some defect report.

Denis
Jul 22 '05 #13
Leor Zolman wrote:
On Wed, 26 May 2004 04:28:58 +0000, Denis Remezov
<RE*********************@yahoo.removethis.ca> wrote:

On the second (or is it the third) thought, 5.5.3, when describing the
operator ->* , mentions that the class type "is a completely-defined class
type". Does that mean that calling a function through a pointer-to-member
of an incomplete type is [formally] UB?

As I said in my response to John, I wasn't sure if I'd somehow managed to
cheat the devil here. It would seem likely I have. But even Comeau accepts
it as written (except for the missing & on taking the address of the
function) ...I've put a query in to Greg and EDG. We'll see what
transpires. But right now it's looking to me a lot like undiagnosed UB,
alas. And I was feeling so proud of myself, too ;-)
-leor


See :

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15684

It appears that this is controversial.

Jul 22 '05 #14
On 27 May 2004 13:10:22 EDT, Gianni Mariani <gi*******@mariani.ws> wrote:
Leor Zolman wrote:
On Wed, 26 May 2004 04:28:58 +0000, Denis Remezov
<RE*********************@yahoo.removethis.ca> wrote:

On the second (or is it the third) thought, 5.5.3, when describing the
operator ->* , mentions that the class type "is a completely-defined class
type". Does that mean that calling a function through a pointer-to-member
of an incomplete type is [formally] UB?

As I said in my response to John, I wasn't sure if I'd somehow managed to
cheat the devil here. It would seem likely I have. But even Comeau accepts
it as written (except for the missing & on taking the address of the
function) ...I've put a query in to Greg and EDG. We'll see what
transpires. But right now it's looking to me a lot like undiagnosed UB,
alas. And I was feeling so proud of myself, too ;-)
-leor


See :

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15684

It appears that this is controversial.


I just replied to the original reply; I think that guy missed the point
entirely. I suspect it won't end up being all that controversial, just
probably something no one's ever tried to do before. And perhaps folks just
aren't used to seeing gcc and EDG both getting the same thing wrong.
-leor
--
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
Jul 22 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Hubert Baumeister | last post by:
Fifth International Conference on eXtreme Programming and Agile Processes in Software Engineering XP2004 June 6-10, 2004, Garmisch-Partenkirchen, Germany http://www.xp2004.org/
0
by: Eugene Safrankow | last post by:
Hello All! I've encountered with the error when I call a method of dependency library (written in managed VC++) from Smart Client placed on a web page. In general, I make a call to the Windows...
23
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
3
by: JoeK | last post by:
Hey all, I am automating a web page from Visual Foxpro. I can control all the textboxes, radio buttons, and command buttons using syntax such as: ...
13
by: Bern McCarty | last post by:
I have run an experiment to try to learn some things about floating point performance in managed C++. I am using Visual Studio 2003. I was hoping to get a feel for whether or not it would make...
3
by: harborboy76 | last post by:
I am calling the exact same stored procedure called myprocedure from 2 different boxes from the CLP, but I'm experiencing different behaviors between them. After I was unable to get any support...
24
by: John | last post by:
I know this is a very fundamental question. I am still quite confused if the program call stack stack should always grows upwards from the bottom, or the opposite, or doesn't matter?? That means...
46
by: Steven T. Hatton | last post by:
I just read §2.11.3 of D&E, and I have to say, I'm quite puzzled by what it says. http://java.sun.com/docs/books/tutorial/essential/concurrency/syncrgb.html <shrug> -- NOUN:1. Money or...
3
by: cberthu | last post by:
Hi all, Is it possible to have two connects in the same rexx script to different DB's? I have to get data form on DB (with specifics selects and filter out some values with RExx) and save the...
2
by: jojoba | last post by:
Hello to all! I have a fairly simple webservice running in asp.net ajax under c# (vs 2008). I built the service and it runs just dandy when i test it by itself in visual studio. However, to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.