Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Inheritance: Can I override base class data members?

Tags:

c++

Let's say I have two classes like the following:

Class A
{
public:
..
private:
  int length;
}

Class B: public Class A
{
public:
..
private:
 float length;
}

What I would like to know is:

  1. Is overriding of base class data members allowed?
  2. If yes, is it a good practice?
  3. If no, what is the best way to extend the type of the data members of a class?

There is a class that satisfies my needs and I want to reuse it. However for my program needs, its data members should be of another type.

I have some books, but all of them refer only to overriding of base class member methods.

like image 506
Harry Avatar asked Feb 02 '09 00:02

Harry


1 Answers

You can use templatized members i.e., generic members instead of overriding the members.

You can also declare a VARIANT(COM) like union.

   struct MyData
   {
        int vt;              // To store the type

        union 
        {                
            LONG      lVal;
            BYTE      bVal;
            SHORT     iVal;
            FLOAT     fltVal;
            .
            .
        }
   };
like image 149
Vinay Avatar answered Oct 03 '22 10:10

Vinay