Parent class to call children method
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Parent class to call children method

  1. #1
    Join Date
    May 2009
    Posts
    7

    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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    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.

  3. #3
    Join Date
    Apr 2008
    Posts
    726

    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 03:03 PM.

  4. #4
    Join Date
    May 2009
    Posts
    7

    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

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: Parent class to call children method

    Quote Originally Posted by smishash View Post
    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
    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.

  6. #6
    Join Date
    May 2009
    Posts
    7

    Re: Parent class to call children method

    Quote Originally Posted by GCDEF View Post
    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 :-)

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,228

    Re: Parent class to call children method

    Quote Originally Posted by smishash View Post
    - shure, i just liked their way to "defence" themselves from unwanted deeper inheritance :-)
    Why?

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    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.

  9. #9
    Join Date
    May 2009
    Posts
    7

    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 04:50 PM. Reason: To clarify the usage:

  10. #10
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center