CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Inheritance: Ensure call of base fucntion from derived class

    Hi,

    I´ve got two classes: Base and Derived. Base offers some virtual (non pure virtual method) that are overwritten by Derived. Is it possible to ensure that Derived calls Base function from the overwritten Derived function?

    Here´s an example:

    Code:
    class Base
    {
    public:
       Base()
       {
       }
       
       virtual Base()
       {
       }
    
       virtual void func1()
       {
          // do some stuff
       }
    
       virtual void func2()
       {
          // do some more stuff
       }
    };
    
    class Derived1 : public Base
    {
    public:
       Derived1()
       {
       }
    
       void func1()
       {
          // OK, calls Base func
          Base::func1();
          
          // do some Derived specific stuff
       }
    
       void func2()
       {
          // oops, forgot to call Base::func2()
          // do some Derived specific stuff
       }
    };
    - Guido

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Inheritance: Ensure call of base fucntion from derived class

    Perhaps this will work:
    Code:
    class Base
    {
    public:
       Base()
       {
       }
    
       virtual ~Base() {}
    
       void func1()
       {
          // do some stuff
          func1_();
       }
    
       void func2()
       {
          // do some more stuff
          func2_();
       }
    
    protected:
       virtual void func1_()
       {
          // do nothing
       }
    
       virtual void func2_()
       {
          // do nothing
       }
    };
    
    class Derived1 : public Base
    {
    public:
       Derived1()
       {
       }
    
    protected:
       void func1_()
       {
          // do some Derived specific stuff
       }
    
       void func2_()
       {
          // do some Derived specific stuff
       }
    };
    The idea is that Base provide hooks for the Derived class to implement.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Inheritance: Ensure call of base fucntion from derived class

    Great suggestion, I think it will fit my needs.
    Thank you

    PS:
    Unfortunately I have no reputation to spread at the moment.
    - Guido

  4. #4
    Join Date
    May 2007
    Location
    Bangalore India
    Posts
    262

    Re: Inheritance: Ensure call of base fucntion from derived class

    Same here.. What do I have to do to get reputation to spread?
    Dont forget to rate my post if you find it useful.

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Inheritance: Ensure call of base fucntion from derived class

    Look out for good posts by various other people and add to their reputation
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Nov 2006
    Location
    Essen, Germany
    Posts
    1,344

    Re: Inheritance: Ensure call of base fucntion from derived class

    Hi again,

    I gave some thought to your suggestion, laserlight, and find it very useful for flat hierachies with only one level of inheritance. How do I implement it for two or more levels, is it possible at all?
    - Guido

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Inheritance: Ensure call of base fucntion from derived class

    I gave some thought to your suggestion, laserlight, and find it very useful for flat hierachies with only one level of inheritance. How do I implement it for two or more levels, is it possible at all?
    EDIT:
    Okay, I did some experimentation, and I think that one cannot really prevent the problem elegantly. Perhaps the best you can do is to provide yet another hook function, and specify that subclasses should implement that instead of the original hook function from the base class.
    Last edited by laserlight; February 29th, 2008 at 08:55 AM.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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