Organizing Inheritance

This is a discussion on Organizing Inheritance within the C++ Programming forums, part of the General Programming Boards category; Here's a bit of pseudocode describing the situation I'm in... Code: class Base{ public: void method(){ //do some stuff protectedFunc(foo); ...

  1. #1
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136

    Organizing Inheritance

    Here's a bit of pseudocode describing the situation I'm in...

    Code:
    class Base{
    public:
         void method(){
              //do some stuff
              protectedFunc(foo);
              //do some stuff with foo
         };
    
    protected:
         void protectedFunc(Foo* foo){
              //do some stuff
         };
    }
    
    
    class Inherited : public Base{
    public:
         void method(){
              //do same stuff as Base
              protectedFunc(foo);
              //do same stuff with foo
         }
    
    protected:
         void protectedFunc(Foo* foo){
              //do some DIFFERENT stuff
         }; 
    }
    Obviously, I could copy and paste the code like I have listed here. Alternatively, I could divide the function into methodPartA(), protectedFunc(), and methodPartB(). This just seems a bit ungainly in terms of appearance. Is there a better way that I'm missing?

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    So if I understand you correctly you want method() to do the same thing in both classes and protectedFunc() to behave differently depending on if it's a base or inherited object?

    Then you could leave out the declaration of method() in Inherited, and declare protectedFunc() as virtual in Base

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    Your design could be correct, except that you should not redefine method in the derived class. You just need to override protectedFunc, which can be declared as a private virtual function in the base class. This is known as the non-virtual interface idiom.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    Thanks. the NVI idiom was what I was looking for. It's been a long day...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditional inheritance?
    By arcaine01 in forum C++ Programming
    Replies: 17
    Last Post: 09-04-2009, 04:12 PM
  2. Replies: 16
    Last Post: 06-08-2009, 03:03 PM
  3. Multiple Inheritance - Size of Classes?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 11-26-2004, 08:04 AM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Inheritance vs Composition
    By Panopticon in forum C++ Programming
    Replies: 11
    Last Post: 01-20-2003, 03:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21