Derived class passing callback to base class

Discussion in 'C++' started by Bit Byte, Mar 5, 2007.

  1. Bit Byte

    Bit Byte Guest

    Must be the time of the day, but I seem to get my head in a spin over
    this ...

    I have a base class B, from which I have a derived class.

    In class B, i have a (public access) typedef of a template class functor
    (lets call it F for convenience)

    I want to declare a variable of type F in class D.

    the desired behaviour (i.e. what I'm after is this) :

    class D calls a method in its base class, passing it a variable of type
    F - and under a particular scenario - for a particular event, I want the
    base class (B) to notify/callback the derived class D (so that
    additional processing can be carried out when that event occurs.

    For some reason, I can't quite implement it. A simple example will be
    appreciated.
     
    Bit Byte, Mar 5, 2007
    #1
    1. Advertising

  2. On Mar 5, 4:49 am, Bit Byte <> wrote:
    > Must be the time of the day, but I seem to get my head in a spin over
    > this ...
    >
    > I have a base class B, from which I have a derived class.
    >
    > In class B, i have a (public access) typedef of a template class functor
    > (lets call it F for convenience)
    >
    > I want to declare a variable of type F in class D.
    >
    > the desired behaviour (i.e. what I'm after is this) :
    >
    > class D calls a method in its base class, passing it a variable of type
    > F - and under a particular scenario - for a particular event, I want the
    > base class (B) to notify/callback the derived class D (so that
    > additional processing can be carried out when that event occurs.
    >
    > For some reason, I can't quite implement it. A simple example will be
    > appreciated.


    You could use a private virtual method in the base-class with some
    default implementation (just returning) and then implement this method
    in the derived class. That way you can have different implementations
    in different derived classes.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 5, 2007
    #2
    1. Advertising

  3. Bit Byte

    Grizlyk Guest

    Bit Byte wrote:
    >
    > class D calls a method in its base class, passing it a variable of type
    > F - and under a particular scenario - for a particular event, I want the
    > base class (B) to notify/callback the derived class D (so that additional
    > processing can be carried out when that event occurs.


    To call method "Derived::A" ( method "Derived::A" is function of derived
    class "Derived" implementing message "A" ( message "A" is declaration of
    class member function "A") ) from other method "Base::B" of base class
    "Base", you can declare the message "A" in base class with the help of
    "virtual" keyword. It is hard to understand what do you want as
    "notify/callback the derived class D".

    > For some reason, I can't quite implement it. A simple example will be
    > appreciated.


    Give the code you have written already and point to error there.


    --
    Maksim A. Polyanin
    http://grizlyk1.narod.ru/cpp_new

    "In thi world of fairy tales rolls are liked olso"
    /Gnume/
     
    Grizlyk, Mar 5, 2007
    #3
  4. Bit Byte

    Bit Byte Guest

    Bit Byte wrote:

    > Must be the time of the day, but I seem to get my head in a spin over
    > this ...
    >
    > I have a base class B, from which I have a derived class.
    >
    > In class B, i have a (public access) typedef of a template class functor
    > (lets call it F for convenience)
    >
    > I want to declare a variable of type F in class D.
    >
    > the desired behaviour (i.e. what I'm after is this) :
    >
    > class D calls a method in its base class, passing it a variable of type
    > F - and under a particular scenario - for a particular event, I want the
    > base class (B) to notify/callback the derived class D (so that
    > additional processing can be carried out when that event occurs.
    >
    > For some reason, I can't quite implement it. A simple example will be
    > appreciated.


    Got round this by slight change in architectural design. Changed
    relation between claases D and C from a Is-A to a Uses-A relation, and
    it became much more simpler to implement he desired behavior.. thanks (I
    just needed a few hours sleep + some strong coffee :p)
     
    Bit Byte, Mar 5, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    9
    Views:
    573
    justanotherguy
    Dec 3, 2004
  2. Jeff
    Replies:
    4
    Views:
    575
    Peter_Julian
    Mar 8, 2006
  3. Replies:
    1
    Views:
    397
    myork
    May 23, 2007
  4. Replies:
    1
    Views:
    389
    Victor Bazarov
    May 23, 2007
  5. Replies:
    2
    Views:
    715
Loading...

Share This Page