473,399 Members | 3,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 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 2629
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
7
by: Tron Thomas | last post by:
Under the right compiler the following code: class Base { public: virtual void Method(int){} }; class Derived: public Base {
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
4
by: Jeff | last post by:
The derived class below passes a reference to an object in its own class to its base calss constructor. The code compiles and will run successfully as long as the base class constructor does not...
15
by: Jeff Mason | last post by:
Hi, I'm having a reflection brain fog here, perhaps someone can set me on the right track. I'd like to define a custom attribute to be used in a class hierarchy. What I want to do is to...
2
by: reckless2k | last post by:
Client side; knows nothing of Derived: class Base { ... virtual void do_something() } #include "Base.h" void main() {
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
5
by: Zeppe | last post by:
Hi all! my problem is this one, I think that it could be a common one, maybe a pattern, so if you can help me somehow it would be great. Let's suppose I have a class Base class Base { //...
3
by: Vulcan | last post by:
<code> class Base { }; class Class1 : public Base{ public: void Function1(); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.