469,082 Members | 1,113 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,082 developers. It's quick & easy.

Derived class passing callback to base class

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.
Mar 5 '07 #1
3 2370
On Mar 5, 4:49 am, Bit Byte <r...@your.box.comwrote:
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

Mar 5 '07 #2

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/
Mar 5 '07 #3


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)
Mar 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Tron Thomas | last post: by
5 posts views Thread by Zeppe | last post: by
3 posts views Thread by Vulcan | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.