How can I force a derived class to override a base class member variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sedrel
    New Member
    • Aug 2009
    • 2

    How can I force a derived class to override a base class member variable?

    Hello All,

    I'm new to C++ (been an Ada guy for over 20 years), and I've search but can't find an answer to my question on the Web (big place!), so I'm hoping you Gurus come help me.

    I know that for virtual functions, if you declare it with an "= 0" (pure virtual) it not only designates the base class as an "abstract class", but also forces the derived class to define an implementation.

    Is there a way to do this for variables? What I really want is something like (not 100% on syntax, so please forgive):

    class Base
    {
    public:
    virtual void dummyMakeMeAbst ract() = 0; // don't allow instantiations

    protected:
    virtual const static typeXXX mustOverrideVar iable;
    }


    class Derived : public Base
    {
    public:
    void dummyMakeMeAbst ract(){};

    protected:
    const static typeXXX mustOverrideVar iable = myValue;
    }

    I could live without the const static behavior if I had to.

    Thanks in advance !!!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    You can't force a derived class to override a base class member variable.

    However looking at what you have written it appears you could easily achieve the behaviour you seem to want using a pure virtual function returning the type you are interested in.

    Comment

    • sedrel
      New Member
      • Aug 2009
      • 2

      #3
      Thanks Banfa, a pure virtual function returning the desired value sounds like it will do just the trick. I should have seen that, since I had to write a "getter" for the variable anyway. Thanks again.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Keep in mind that all a virtual function means is that you cannot call that function using an object of that class. To force this, the compiler will not allow objects of that class to prevent your trying to call the pure virtual function. This is what is meant by an abstract class.

        However, you can call that function in a derived class:
        Code:
        class Base
        {
            public:
               int virtual fx() = 0 { return 100;}
        };
        
        class Derived :public Base
        {
        
            private:
                 int virtual fx() { return Base::fx();}   //OK
             
        };

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          a. I think you mean pure virtual function in your first sentence (probably)

          b. I never knew you could used syntax like that so today I have learned something, thank you.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            a. Yes, I meant pure virtual function.

            b. Just like a polymorphic base class contains the common methods for the inheritance hierarchy, a base class virtual function can contain the common processing for a given method that is to be used in each derived class override. Whether the function is a pure virtual or not is irrelevant.

            Comment

            Working...