tags:

views:

721

answers:

5

Lets 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. Thanks everybody in advance.

+6  A: 

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;
            .
            .
        }
   };
Vinay
And if you want more functionality for a certain class (similar to subclassing), can he use template specialization?
strager
yes he can specialize the templates
Vinay
This technically isn't overriding ;)
OJ
No need to override the members if members are generic for his case.Moreover it is a bad practice to override the members.
Vinay
Totally agree (see my answer below). Changing meaning of data members is a fail. But creating template instances isn't overriding :)
OJ
A: 

private means that the data cannot be touched, even by derived classes. Protected means that the derived classes can use the data-- so the length of class A won't affect length of class B. Having said that, it's probably best to have a virtual function GetLength() that's overridden by the derived class.

(as has been pointed out, I'm wrong-- you can't use GetLength on different types, so if the base class is int and the derived class is float, then that's not going to work).

mmr
You cannot change the signature to override a virtual member function. Doing so would create a different virtual function.
strager
Maybe I'm explaining it badly-- if you have a virtual member function, the derived class can override the virtual function. The derived class then uses the derived method.
mmr
I know that. However, try compiling "virtual int GetLength()" in class A and "virtual float GetLength()" in class B.
strager
ah, good point. My bad.
mmr
+3  A: 

1) No you can't. You can create your own internal/hidden/private members, but you can't overide them.

2) If you could, not it wouldn't be good practice. Think about it ;)

3) You shouldn't, as you're changing the meaning of the member.

OJ
Think about virtual functions: aren't you changing the meaning of a call? However, I still agree with you; functions are valled, but members are used.
strager
I agree with redefining behaviour based on type. I do not agree with changing the underlying meaning of a data item. :)
OJ
+2  A: 

While declaring a data member of the same name in a derived class is legal C++, it will probably not do what you intend. In your example, none of the code in class A will be able to see the float length definition - it can only access the int length definition.

Only methods can have virtual behaviour in C++, not data members. If you would like to reuse an existing class with another data type, you will have to either reimplement the class with your desired data type, or reimplement it as a template so you can supply the desired data type at instantiation.

Greg Hewgill
A: 

1) yes it is allowed, as in, you can do it

2) No! bad practice. If someone calls a method that use 'length' the variable being returned will be undefined.

3) Try a different design. Perhaps you want to have a similarly named function or use the baseclass as a "has-a" class instead of an "is-a" class.

cbrulak