472,127 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

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

2
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 dummyMakeMeAbstract() = 0; // don't allow instantiations

protected:
virtual const static typeXXX mustOverrideVariable;
}


class Derived : public Base
{
public:
void dummyMakeMeAbstract(){};

protected:
const static typeXXX mustOverrideVariable = myValue;
}

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

Thanks in advance !!!
Aug 11 '09 #1
5 8256
Banfa
9,065 Expert Mod 8TB
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.
Aug 12 '09 #2
sedrel
2
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.
Aug 12 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
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:
Expand|Select|Wrap|Line Numbers
  1. class Base
  2. {
  3.     public:
  4.        int virtual fx() = 0 { return 100;}
  5. };
  6.  
  7. class Derived :public Base
  8. {
  9.  
  10.     private:
  11.          int virtual fx() { return Base::fx();}   //OK
  12.  
  13. };
Aug 12 '09 #4
Banfa
9,065 Expert Mod 8TB
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.
Aug 12 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
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.
Aug 13 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

24 posts views Thread by Shao Zhang | last post: by
9 posts views Thread by Daniel Kay | last post: by
3 posts views Thread by Immortal Nephi | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.