Thread: Organizing Inheritance

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

    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,138
    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, 09: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, 04:41 AM