Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Derived class passing callback to base class

Reply
Thread Tools

Derived class passing callback to base class

 
 
Bit Byte
Guest
Posts: n/a
 
      03-05-2007
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.
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Erik_Wikstr=F6m?=
Guest
Posts: n/a
 
      03-05-2007
On Mar 5, 4:49 am, Bit Byte <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Grizlyk
Guest
Posts: n/a
 
      03-05-2007

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/


 
Reply With Quote
 
Bit Byte
Guest
Posts: n/a
 
      03-05-2007


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 )
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting from base to derived class in base constructor pastbin@gmail.com C++ 2 02-07-2008 02:41 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
Passing reference to derived class object to base class constructor Jeff C++ 4 03-08-2006 03:14 AM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM



Advertisments