-
May 30th, 2009, 06:52 AM
#1
Parent class to call children method
Hello all,
I am looking for elegant way to call children's method from parent class,
is at possible at all?
Thanks
-
May 30th, 2009, 07:32 AM
#2
Re: Parent class to call children method
Use virtual functions if they're appropriate. A parent class shouldn't have any knowledge of classes derived from it.
-
May 30th, 2009, 02:00 PM
#3
Re: Parent class to call children method
if the parent has declared a virtual, and the parent calls that method somewhere in the class, it will actually call the child class's override of the virtual. oops, just like gcdef said.
otherwise you might need to start thinking about call back maps etc.
Last edited by Amleto; May 30th, 2009 at 02:03 PM.
-
May 30th, 2009, 02:35 PM
#4
Re: Parent class to call children method
Gcdef,Amleto, thanks for your advise,
so after some googling I found a solution that seems nice - using private inheritance
and pure virtual functions at the base class.
http://www.parashift.com/c++-faq-lit....html#faq-24.3
-
May 30th, 2009, 02:47 PM
#5
Re: Parent class to call children method
Originally Posted by smishash
I don't see that private inheritance and pure virtuals buy you anything in this case. public inheritance and regular virtual functions should do what you need.
-
May 30th, 2009, 03:00 PM
#6
Re: Parent class to call children method
Originally Posted by GCDEF
I don't see that private inheritance and pure virtuals buy you anything in this case. public inheritance and regular virtual functions should do what you need.
- shure, i just liked their way to "defence" themselves from unwanted deeper inheritance :-)
-
May 30th, 2009, 03:59 PM
#7
Re: Parent class to call children method
Originally Posted by smishash
- shure, i just liked their way to "defence" themselves from unwanted deeper inheritance :-)
Why?
-
May 31st, 2009, 06:49 AM
#8
Re: Parent class to call children method
If private inheritance seems suitable for your case then probably, you should take the simpler route of composition. That would give you the flexibility of having 1-to-many relationship with the class and the member it is composed of. With private inheritance you would have a 1-1 relationship of the derived and the base object. But, the decision is yours, because we don't know what you are trying to do exactly. May be you don't need inheritance or may be you need public inheritance or may be you don't need virtual functions at all. Anything could be possible in the dark context we have.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
May 31st, 2009, 03:36 PM
#9
Re: Parent class to call children method
I would like to show the simplified case which describes my goal.In general there is
some system with feedback, and the difference between data collecting and the feedback
is just in parameters.
There are many child classes of "Measurement", I just would' like them to deal with "Feedback", but just to decide what type
of "Measurement" to do - with feedback or without.
Something like that:
#include <iostream>
using namespace std;
typedef struct _PARAM {
char* param1;
char* param2;
} PARAM;
class Feedback {
protected:
virtual void get_measurement()=0;
public:
void Feedback_main() {
get_measurement();
}
};
class Measurement : private Feedback {
protected:
public:
Measurement *_feedbck;
char* str_out;
void get_measurement() {
cout << "get_measurement() with " << str_out << endl;
}
void get_measurement_with_Feedback(PARAM parameters) {
_feedbck = new Measurement;
str_out = parameters.param1;
get_measurement();
_feedbck->str_out=parameters.param2;
_feedbck->Feedback_main();
}
};
int main() {
Measurement *meas = new Measurement;
PARAM parameters;
parameters.param1="Measurement parameters";
parameters.param2="Feedback parameters";
meas->get_measurement_with_Feedback(parameters);
}
******************
the output is:
get_measurement() with Measurement parameters
get_measurement() with Feedback parameters
Last edited by smishash; May 31st, 2009 at 03:50 PM.
Reason: To clarify the usage:
-
June 2nd, 2009, 03:41 AM
#10
Re: Parent class to call children method
Code:
class Measurement : private Feedback {
protected:
public:
Measurement *_feedbck;
char* str_out;
Actually you coding composition.
Thanks for your help.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|