Sunday, March 27, 2011

In Inheritance: Can I override base class data members??

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.

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

    strager : You cannot change the signature to override a virtual member function. Doing so would create a different virtual function.
    mmr : 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.
    strager : I know that. However, try compiling "virtual int GetLength()" in class A and "virtual float GetLength()" in class B.
    mmr : ah, good point. My bad.
  • 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.

    strager : 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.
    OJ : I agree with redefining behaviour based on type. I do not agree with changing the underlying meaning of a data item. :)
  • 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.

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

0 comments:

Post a Comment