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

Hybrid View

  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,635

    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
    725

    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.

  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,635

    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,635

    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
    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
  •  





Click Here to Expand Forum to Full Width

Featured