1

Can polymorphism be used on a static data member of a C++ class (a.k.a. "class variable")?

Update: As b4hand stated, polymorphic methods must be declared in the base class. But that won't be possible in this scenario because it is not known in advance what methods the user will create in containerDerived.

I edited the example to include references; thank you for reminding me.

Here is my attempt to initialize a static base type (numBase) to a derived type (numDer):

#include <iostream>

class numBase                   //numBase is in a library user can not edit
{
};

class numDer : public numBase   //user defined class is a kind of numBase
{
    private:
        int num;
    public:
        void printNum() { std::cout << " numDer=" << num; }
        void inc() { num++; }
};

class containerBase             //containerBase is in a library user can not edit
{
    protected:
        //static numDer& count; //this compiles, but count can not be initialized to other types
        static numBase& count;  //this causes error six lines down from here
};

class containerDerived : public containerBase //user defined class is a kind of containerBase
{
    public:
        void inc() { count.inc(); } //error: 'count' was not declared in this scope
        void printCount() { std::cout << " containerDerived"; count.printNum(); }
};

/************************ user program **********************/
//initilialize static variable
numDer number;              //number could be any user defined type derived from numBase
numDer& containerBase::count = number; //initialize count to a kind of numBase

int main()
{
    containerDerived container1;
    containerDerived container2;

    container1.printCount();
    container1.inc();
    container2.inc();
    container1.printCount();
}

Thank you.

3
  • numBase is an empty class. It has no inc() method. The compiler error is quite descriptive here, can you elaborate more on what your actual issue is?
    – Red Alert
    Nov 15, 2014 at 7:53
  • If you want polymorphism, use pointers like everywhere else.
    – molbdnilo
    Nov 15, 2014 at 7:54
  • You can't, because count is a numBase instance, not a numBase pointer. Nov 15, 2014 at 7:54

2 Answers 2

1
static numBase count;   

You have declared count to be an object of numBase. And through that object you are trying to call member of derived class of numBase i.e numDer.inc(). This call surely won't succeed as run time polymorphism is only suitable when pointers/references are used.

0

You can't do what you're trying to do in the way you are trying to do it.

Assigning a numDer to a numBase will "slice" the object and no longer be the correct type.

In order to get polymorphic behavior, you must use either pointers or references.

The compiler is reporting an error correctly telling you the inc method does not exist on numBase.

Polymorphic methods must be declared in the base class and also declared virtual.

2
  • "must" is a bit of a strong word - they should be declared virtual. And so should the base's destructor. In the case that you never plan to declare a base case as a pointer/reference to a derived class, the virtual declaration is unnecessary altogether. Omitting the virtual keyword may make your program faster as well, given the optimizer.
    – Red Alert
    Nov 15, 2014 at 8:13
  • You won't actually get polymorphic behavior on the method unless it is declared virtual. Instead you get a shadowed method and if a pointer to the base is invoked you always get the base implementation. So if the desired behavior is polymorphism then "must" is appropriate. Since the question is both tagged and states polymorphism, it certainly sounds like "must" applies. Also your statement about running faster is useless if the wrong method is invoked and incorrect behavior occurs.
    – b4hand
    Nov 15, 2014 at 8:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.