Re: Getting the instance pointer from a called function

From: Elias Athanasopoulos (elathan_at_phys.uoa.gr)
Date: 03/23/04


Date: Tue, 23 Mar 2004 21:38:41 +0200


On Tue, 23 Mar 2004, Jonathan Turkanis wrote:
> "Elias Athanasopoulos" <elathan@phys.uoa.gr> wrote in message
> > Consider I have a Foo class, which ctor's takes as an argument a
> pointer
> > to function, i.e.:
> >
> > Foo *foo = new Foo(bar);
> >
> > Where:
> >
> > int bar(int);
> >
> > bar() will eventually get called somewhen inside Foo (it maybe
> called N
> > times).
> >
> > Now, I want to grab the pointer of Foo's instance (i.e. the 'this'
> > pointer) which called bar(), from inside bar(), when that happens.
>
> Before invoking bar, store a pointer to the current instance of Foo in
> a place where it is accessible to bar, e.g. in a static member, or in
> thread-local storage if you are writing a multithreaded application.

The problem is that I cannot modify Foo. I can have a global array which
will store pointers of Foo instances, but bar() has a strict prototype and
will not be able to identify from which instance is called. bar() has a
per-instance behaviour.

Regards,
Elias



Relevant Pages

  • Re: whats a callback?
    ... Rene may have ... 'foo' needs to be written now, which has to call another function ... 'bar' that may not exist yet. ... So you declare the interface signature of 'bar', and make a pointer to ...
    (comp.arch.embedded)
  • Re: whats a callback?
    ... Rene may have ... 'foo' needs to be written now, which has to call another function ... 'bar' that may not exist yet. ... So you declare the interface signature of 'bar', and make a pointer to ...
    (sci.electronics.design)
  • Re: "Mastering C Pointers"....
    ... > Windows shortcut, there is definitely an analogy there. ... a pointer variable holds an address of an object. ... int foo = 12; ... int *bar; ...
    (comp.lang.c)
  • Re: Interview questions
    ... and note that this means the implementor of Bar needs to ... Bar might want to inherit some virtual methods of Foo. ... is the first member of bar, we can say a Bar "is a" Foo. ... > You cannot pass a pointer to an object of type Bar ...
    (comp.lang.cpp)
  • Re: Circular dependency - I think..
    ... > I see that you are not initialising the pointer to BAR contained in FOO. ...
    (comp.lang.cpp)