Derived class passing callback to base class

B

Bit Byte

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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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.
 
G

Grizlyk

Bit said:
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/
 
B

Bit Byte

Bit said:
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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top