473,394 Members | 1,737 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 8609
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

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

Similar topics

6
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
24
by: Shao Zhang | last post by:
Hi, I am not sure if the virtual keyword for the derived classes are required given that the base class already declares it virtual. class A { public: virtual ~A();
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
2
by: Joe HM | last post by:
Hello - I have a function in a base class that I want to use the shadow'ed member variable of the derived class. Here is the code ... Public Class cCaptureBase Protected Const cDummy As...
1
by: archana | last post by:
Hi all, I am confuse in concept of inheritance. I am having following 2 classes class base1 { public void abc() { System.Console.WriteLine("base1 abc"); }
6
by: Hubert Fritz | last post by:
Hello, I fear I want to have something which is not possible in C++. How is it possible to define a base class so that the derived class is forced to contain a static member variable, which...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
3
by: Immortal Nephi | last post by:
The rule of inheritance states that you define from top to bottom. Sometimes, you want to define base class and set reference from dervied class to base class, but you violate the rule. Here is an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.