Class creating another class and needing callback function 
Author Message
 Class creating another class and needing callback function

Hi,

I have a strange situation here that I hope someone can advise me on.  I
have not been developing in C++ for too long so I hope I can get around
this problem in some way I haven't thought of.

I have an instantiated object of type A as follows:

class A {
  B *dat;

    Foo() {
      ...
      dat = new B;
      ...
      // how can a method in B call the method Bar() ?
    }

  Bar() {..};

Quote:
}

A *thing = new A;

You see it creates an object of type B, whose class definition is as
follows:

class B {
  Proc() {
    // how can I call the method Bar in the object "thing"
    // of type A here?
  }

Quote:
}

So, I have an object of type A, which creates an object of type B (say, a
timer method).  Then B wants to call a function in class A (say, a
callback function or event handler of some sort).

So, how can I do this?  I would like the object of type B to somehow
"know" of the "parent" (not parent in the conventional C++ definition)
object that created it (here, thing) and call a member function (here,
Bar) of that parent object.  But for various reasons I cannot do the
simple thing and pass a pointer to the parent into the constructor for B
and dereference it.

Any help with this would be much appreciated.

Thanks,

Mark W.



Sat, 05 Jan 2002 03:00:00 GMT  
 Class creating another class and needing callback function

Quote:

>Hi,

>I have a strange situation here that I hope someone can advise me on.  I
>have not been developing in C++ for too long so I hope I can get around
>this problem in some way I haven't thought of.

>I have an instantiated object of type A as follows:

>class A {
>  B *dat;

>    Foo()
>      ...
>      dat = new B;
>      ...
>      // how can a method in B call the method Bar() ?
>    }

>  Bar() {..};

>}

>A *thing = new A;

>You see it creates an object of type B, whose class definition is as
>follows:

>class B {
>  Proc() {
>    // how can I call the method Bar in the object "thing"
>    // of type A here?
>  }

>}

>So, I have an object of type A, which creates an object of type B (say, a
>timer method).  Then B wants to call a function in class A (say, a
>callback function or event handler of some sort).

>So, how can I do this?  I would like the object of type B to somehow
>"know" of the "parent" (not parent in the conventional C++ definition)
>object that created it (here, thing) and call a member function (here,
>Bar) of that parent object.  But for various reasons I cannot do the
>simple thing and pass a pointer to the parent into the constructor for B
>and dereference it.

>Any help with this would be much appreciated.

>Thanks,

>Mark W.

Can there be multiple A objects or will A be a singleton? If there can only
ever be one A than make Bar() static.

If there can be multiple A objects than a constructed B object needs to know
which A it is 'attached' to. This suggests constructing B objects with the
'parent' A object's 'this' pointer.

I can't guess what your "various reasons" for not doing this might be but I
don't see a way around storing something in B that uniquely identifies its A
object.



Sat, 05 Jan 2002 03:00:00 GMT  
 Class creating another class and needing callback function
Worlds collided and thoughts were forged when Mark W declared:

Quote:
>Hi,
>I have a strange situation here that I hope someone can advise me

<snip>

Quote:
>Any help with this would be much appreciated.

>Thanks,
>Mark W.

Umm, I'm fairly certain there's a design pattern that covers this
very issue. The basic idea is to split up your classes by adding
mediators and whatnot. I could come up with an ad hoc solution
but it wouldn't be pretty. I'm really curious as to why you can't
simply pass a pointer, and if possible, work a way to alleviate
this problem. And failing that, use static functions.

--

Author: Albert [tcm - evil wombat - cheeze]
[discover euphony] Euphony: The Orange
home.earthlink.net/~thecheezeman/euphony/intro.htm

"LOGIC, n. The art of thinking and reasoning
 in strict accordance with the limitations and
 incapacities of the human misunderstanding."
 - Ambrose Bierce, "The Devil's Dictionary"

Hi! I'm a .signature virus! Copy me into yours and join the fun!



Sun, 06 Jan 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Passing C++ Class Member Function to as a C Callback Function Parameter

2. class member function as callback function

3. How to use member function of a C++ class as a callback function

4. need help making class object visible to rest of class functions

5. View class functions needed by other class

6. Callback from unmanaged class to managed class...

7. Accessing class member functions from Global Callback

8. CALLBACK function within class ???

9. Use of callback function in a C++ class of a ActiveX control

10. Class memberfunction as DDE callback function

11. Callback function in a class?

12. CALLBACK function as a method of class?

 

 
Powered by phpBB® Forum Software