472,142 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 2528
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
reply views Thread by leo001 | last post: by

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.