473,837 Members | 1,653 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Call child class methods in parent class

Hello,

I would like to implement a callback mechanism in which a
child class registers some methods with particular signatures
which would then be called in a parent class method.

In half-code this should in the end look like this:

In the child class:

| int ChildClass::foo Handler(int a, char b)
| {
| //[...]
| }
|
| int ChildClass::bar Handler(int a, char b)
| {
| //[...]
| }
|
|
| void ChildClass::ini t (void)
| {
| registerMethod ("foo", &fooHandler) ;
| registerMethod ("bar", &barHandler) ;
| }

In the parent class:

| int ParentClass::ru nHandler (const char *tag, int a, char b)
| {
| HandlerType *handler = findMethod (tag);
| return (*handler) (a, b);
| }

In the real life problem, the code in the parent class is of
course much more complex. In the existing implementation this
is all done in the child classes, so that we got a lot of
unnecessary redundance in the code.
The problem is that because different child classes would
define different numbers of such methods (and thus also of
different names) using abstract methods does not seem to help
here.

Has anybody managed to get something like this running?

Cheers,

Martin

--
manche meinen, lechts und rinks kann man nicht velwechsern.
werch ein illtum! -- elnst jandr
-=-=- -=-=-=-=- --=-=-
Dipl. Ing. Martin Dietze -=-=- http://www.the-little-red-haired-girl.org
Jul 23 '05 #1
9 5131
Martin Herbert Dietze wrote:
[snip]
Has anybody managed to get something like this running?


What is your particular problem *exactly*.
Do you get error messages from the compiler or do
you have other problems?
What you posted seems like the beginning of a road which
leeds to a solution.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 23 '05 #2
Martin Herbert Dietze wrote:
I would like to implement a callback mechanism in which a
child class registers some methods with particular signatures
which would then be called in a parent class method.
What is by your definition a "child" and what's a "parent"?
Examples: the child class derives from the parent, or the parent
class object contains a child object (or a pointer), or ???

The reason I ask is that C++ does not have terms "child" or "parent"
and everybody seems to have their own definition of those terms. I
would like to know yours before attempting to give advice.
In half-code this should in the end look like this:

In the child class:

| int ChildClass::foo Handler(int a, char b)
| {
| //[...]
| }
|
| int ChildClass::bar Handler(int a, char b)
| {
| //[...]
| }
|
|
| void ChildClass::ini t (void)
| {
| registerMethod ("foo", &fooHandler) ;
| registerMethod ("bar", &barHandler) ;
| }

In the parent class:

| int ParentClass::ru nHandler (const char *tag, int a, char b)
| {
| HandlerType *handler = findMethod (tag);
| return (*handler) (a, b);
So, how is 'HandlerType' defined? It seems that it's a pointer to
a function. That suggests that 'fooHandler' and 'barHandler' are
static member functions of 'ChildClass'. If not, please elaborate.
| }

In the real life problem, the code in the parent class is of
course much more complex. In the existing implementation this
is all done in the child classes, so that we got a lot of
unnecessary redundance in the code.
I don't know what that all means. Is it really relevant?
The problem is that because different child classes would
define different numbers of such methods (and thus also of
different names) using abstract methods does not seem to help
here.
What abstract methods? Whose abstract methods?
Has anybody managed to get something like this running?


I am fairly certain that many folks have done something similar.
However, you didn't specify enough to understand what to give as
an example.

V
Jul 23 '05 #3
Victor Bazarov <v.********@com acast.net> wrote:
What is by your definition a "child" and what's a "parent"?
Examples: the child class derives from the parent, or the parent
class object contains a child object (or a pointer), or ???


No, I am talking about inheritance. `ChildClass' is derived from
`ParentClass'.
| int ParentClass::ru nHandler (const char *tag, int a, char b)
| {
| HandlerType *handler = findMethod (tag);
| return (*handler) (a, b);


So, how is 'HandlerType' defined? It seems that it's a pointer to
a function. That suggests that 'fooHandler' and 'barHandler' are
static member functions of 'ChildClass'. If not, please elaborate.


The snippet above tries to explain what I want in the end.
Thus, I did not define `HandlerType' in this context. Having a
proper definition for `HandlerType' so that it can be used to
call methods of a derived class in a base class, would actually
be a huge step towards solving my problem.
In the real life problem, the code in the parent class is of
course much more complex. In the existing implementation this
is all done in the child classes, so that we got a lot of
unnecessary redundance in the code.


I don't know what that all means. Is it really relevant?


Only for people asking the "this does not look like too much
redundancy, why do you want to go through all this?" question.
Strictly speaking it is not.
The problem is that because different child classes would
define different numbers of such methods (and thus also of
different names) using abstract methods does not seem to help
here.


What abstract methods? Whose abstract methods?


E.g. `virtual void method() = 0;' in the base class. Since the
derived classes would have to implement it the method can be
used in base class code.
Has anybody managed to get something like this running?


I am fairly certain that many folks have done something similar.
However, you didn't specify enough to understand what to give as
an example.


The problem is about generating different tree structures in
specific parsers which are all derived from a generic parser.
Experience has shown that many of the specific parsers look
pretty similar, so that I hope to put more intelligence into
the base class.
I would thus like to register handler methods for specific
attributes in the specific parsers which would then be called
in the generic one. Does that make more sense to you?

Cheers,

Martin

--
Yoda of Borg I am. Assimilated you will be.
-=-=- -=-=-=-=- --=-=-
Dipl. Ing. Martin Dietze -=-=- http://www.the-little-red-haired-girl.org
Jul 23 '05 #4
Martin Herbert Dietze wrote:
[...]
The problem is about generating different tree structures in
specific parsers which are all derived from a generic parser.
OK. I can be wrong, but AFAIK, it has been solved in many parsers.
Experience has shown that many of the specific parsers look
pretty similar, so that I hope to put more intelligence into
the base class.
Intelligence. OK, I'll bite. What intelligence?
I would thus like to register handler methods for specific
attributes in the specific parsers which would then be called
in the generic one. Does that make more sense to you?


Again, this is too generic. Are you talking about XML processing?
If yes, look at SAX. If you're talking about something not
necessarily limited to processing XML, then what? If you don't
have a model yet, how can we help? Ask maybe in comp.object. If
you do have a model, please describe it. We can then try to come
up with the C++ code for it.

At this junction I can only see the requirement to register some
kind of callback so that it is invoked if some condition is met.
There are plenty of "register -- call if encountered" schemes out
there, choose any you like. Even MFC has one.

V
Jul 23 '05 #5
Martin Herbert Dietze wrote:
Victor Bazarov <v.********@com acast.net> wrote:

No, I am talking about inheritance. `ChildClass' is derived from
`ParentClass'.
| int ParentClass::ru nHandler (const char *tag, int a, char b)
| {
| HandlerType *handler = findMethod (tag);
| return (*handler) (a, b);


So, how is 'HandlerType' defined? It seems that it's a pointer to
a function. That suggests that 'fooHandler' and 'barHandler' are
static member functions of 'ChildClass'. If not, please elaborate.


The snippet above tries to explain what I want in the end.
Thus, I did not define `HandlerType' in this context. Having a
proper definition for `HandlerType' so that it can be used to
call methods of a derived class in a base class, would actually
be a huge step towards solving my problem.


Maybe you can use this library:
http://www.codeproject.com/cpp/FastDelegate.asp

A.

Jul 23 '05 #6
"Martin Herbert Dietze" <he*****@spamco p.net> a écrit dans le message
de
news: d5**********@ww w.fh-wedel.de...
Hello,

I would like to implement a callback mechanism in which a
child class registers some methods with particular signatures
which would then be called in a parent class method.

In half-code this should in the end look like this:


Have a look at Loki's Functor class.

class Parent
{
public:
typedef Loki::Functor<i nt, TYPELIST_2(int, char)> handler;
handler foo;
void trigger_foo(int i, char c)
{
if (foo)
{
foo(i, c);
}
}
};
class Child : public Parent
{
public:
Child()
{
foo = handler(this, &Child::test );
}

int test(int i, char c)
{

}
};

Jonathan

Jul 23 '05 #7
Victor Bazarov <v.********@com acast.net> wrote:
Again, this is too generic. Are you talking about XML processing?
If yes, look at SAX. If you're talking about something not
necessarily limited to processing XML, then what? If you don't
have a model yet, how can we help? Ask maybe in comp.object. If
you do have a model, please describe it. We can then try to come
up with the C++ code for it.
Look, this is the kind of discussion I wanted to avoid, this is
not about the C++ programming language. There may be third
party software that can help me build my application, but this
is not what I am asking for. The question boils down to this:

How can I implement and register callback functions in a
derived class that can then be called in its base class?
At this junction I can only see the requirement to register some
kind of callback so that it is invoked if some condition is met.


Of course this is the way to go. However, a non-static member
function carries its object's type information as part of its
signature. Therefore calling a callback member function of a
derived class raises a technical problem - how can it be done
in C++?

Cheers,

Martin

--
(9) For all resources, whatever it is, you need more.
---RFC 1925 "The Twelve Networking Truths"
-=-=- -=-=-=-=- --=-=-
Dipl. Ing. Martin Dietze -=-=- http://www.the-little-red-haired-girl.org
Jul 23 '05 #8
Jonathan Mcdougall <jo************ ***@gmail.com> wrote:
Have a look at Loki's Functor class.


This looks very good indeed. Having just read a lot about
member-function callbacks in C++ (also thanks to the
documentation coming with FastDelegate and Boost) it seems
to me that this involves a lot of magic. At least good to
know that it can be made to work :-)

Cheers,

Martin

--
Es gibt Themen genug in Deinem eigenen Leben, und wenn sie einmal ausgehn,
gibt es Themenlaeden. -- Die Sterne
-=-=- -=-=-=-=- --=-=-
Dipl. Ing. Martin Dietze -=-=- http://www.the-little-red-haired-girl.org
Jul 23 '05 #9
Martin Herbert Dietze wrote:
[...]
Of course this is the way to go. However, a non-static member
function carries its object's type information as part of its
signature. Therefore calling a callback member function of a
derived class raises a technical problem - how can it be done
in C++?


In the following example the classes are related. They don't
need to be.

----------------------------------------------
#include <string>
#include <map>

class A;
class B {
typedef void (A::*pothermemb er)(int,double) ;
typedef std::map<std::s tring,pothermem ber> mymap;
mymap mp;
public:
B() {}
void registercallbac k(std::string const& s, pothermember);
void callback(std::s tring const& s, A*, int, double);
};

void B::registercall back(std::strin g const& s, pothermember p)
{
mp[s] = p;
}

void B::callback(std ::string const& s, A* pa, int i, double d)
{
pothermember p = 0;
mymap::iterator it = mp.find(s);
if (it != mp.end())
p = it->second;
if (p && pa)
(pa->*p)(i, d);
}

#include <iostream>

class A : public B {
void foo(int i, double d) {
std::cout << "A::foo(" << i << ' ' << d << ")\n";
}
public:
A() { this->registercallba ck("foo", &A::foo); }
};

int main()
{
A a;
a.callback("foo ", &a, 42, 3.1415926);
}
----------------------------------------------

V
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3840
by: pablo | last post by:
Dear NewsGroupers, I am relatively new to OOP and cannet get my head around this problem. I have two classes. Class Child extends Parent. There is no constructor for the Child class. So when I create a child object the constructor for the parent object is called. That works fine. But now I have the problem that I want to add an already existing Parent object to create a new Child object. How can this be done?
2
3878
by: lkrubner | last post by:
My code was dying on the line below where I use method_exists: if (class_exists($nameOfClassToBeUsed)) { $object = new $nameOfClassToBeUsed(); $this->arrayOfAllTheObjectsSoFarLoaded = & $object; if (method_exists($object, "setCallingCode")) $object->setCallingCode($nameOfFunctionOrClassCalling); return $object; } else {
2
10213
by: PawelR | last post by:
Hello Group, In my application I have few class, and I want call function with "master class". This is as master form (startClass) and option window (ClassA). My question is how call function from startClass in classA? public startClass : System.Windows.Forms.Form { static void Main() {...}
4
6535
by: ad | last post by:
base only can call the parent 's method The result of the example below is I am Parent I am Child How can we call the grandparent's method in a inhreited class, I want the result is I am GrandParent
6
6082
by: reandeau | last post by:
I'm building out a OO based app in PHP 5 but I'm getting a little confused on children contructing parents. I have a parent that looks like this: abstract Class State { protected $database; protected $user; protected $output; public function __construct($database,$user,$output) { $this->database = $database;
7
5825
by: Sheikko | last post by:
Hi all, i have an application and i wan to call another program into it, like notepad, so when i minimize the notepad, or maximize, it must rest in my application. thank you
2
1850
by: Jack | last post by:
Hello, I have a 3-level class hirarchy: 1=Grandparent 2=Parent 3=Child The Grandparent has a method called OnPaint which I want to override from the Child class. I can do this, but how can I call the
1
2810
by: someone help | last post by:
hi, i am very new in the software field. i have two child forms. i want two display image in picture box in one child form. when test is completed this image is displayed. i created one class and calling all methods of parent form through the delegate event. i want to call method of child form in same class. how it is possible? please replay.
1
6477
by: Beamor | last post by:
function art_menu_xml_parcer($content, $showSubMenus) { $doc = new DOMDocument(); $doc->loadXML($content);//this is the line in question $parent = $doc->documentElement; $elements = $parent->childNodes; need help. my site worked fine on my localhost but when i uploaded it to my live server i keep getting this error need help to recode line to match my hosting server. im using php 5 i have attached the common_method file
0
9683
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10883
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9412
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7814
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7007
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5852
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4479
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4050
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.