Thread: Using values from derived class in base class

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    7

    Using values from derived class in base class

    I have multiple classes derived from a single base class, and each derived class uses an identical computation function -- except for one variable that is dependent on the derived class. It would be nice to have this shared function written once in the base class and used by each derived class, while using the value from the derived class.

    I'm wondering if there is a more obvious/intuitive or smarter way to do this than what I came up with.

    I wrote a virtual "get" function in the base class, and filled that in for each derived class, so that the base class can call it in the shared computation function.

    As a code example:

    Base class:
    Code:
    /* base.hpp */
    #ifndef BASE_H
    #define BASE_H
    
    class Base
    {
        private:
            /* Defined in each Derived class to return a particular value */
            virtual int getData() {};
    
        public:
            /* Do some computation using the Derived
             * class' implementation of getData */
            int computeThings() { return getData() * 123; };
    };
            
    #endif /* BASE_H */
    Derived class:
    Code:
    /* derived.hpp */
    
    #ifndef DERIVED1_H
    #define DERIVED1_H
    
    #include "base.hpp"
        
    class Derived1 : public Base
    {
        private:
            enum
            {
                /* This derived class uses a data value of 1, any
                 * other classes could use something else */
                data = 1
            };
    
            int getData() { return data; };
    
        public: 
    };
            
    #endif /* DERIVED1_H */
    So there could be a Derived1 class, a Derived2 class, and so on, each pulling in a different "data" variable when they use the Base class' computeThings() function.

    This does what I need it to do, but I'm curious if there is a better way? Thanks for reading.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by gratuitous_arp
    I wrote a virtual "get" function in the base class, and filled that in for each derived class, so that the base class can call it in the shared computation function.
    This is an idiomatic solution, i.e., you are using the non-virtual interface idiom, which in the language of design patterns is an instance of the template method pattern.

    By the way, the base class destructor should be declared virtual, to cater to the possibility that you might destroy a derived class object through a base class pointer.
    Last edited by laserlight; 10-23-2011 at 09:28 AM.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  2. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  3. finding derived class type of a pointer to a base class
    By LinuxCoder in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2006, 11:08 AM
  4. Replies: 2
    Last Post: 04-06-2005, 07:25 AM
  5. Replies: 1
    Last Post: 12-11-2002, 10:31 PM