Connecting Tech Pros Worldwide Help | Site Map

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

Newbie
 
Join Date: Aug 2009
Posts: 2
#1: Aug 12 '09
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 !!!
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,156
#2: Aug 12 '09

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


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.
Newbie
 
Join Date: Aug 2009
Posts: 2
#3: Aug 12 '09

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


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.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,363
#4: Aug 12 '09

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


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. };
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,156
#5: Aug 13 '09

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


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.
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Posts: 5,363
#6: Aug 13 '09

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


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.
Reply