Is it possible to declare a virtual static constant value in a C++ class?

Siska Ádám picture Siska Ádám · Jun 6, 2012 · Viewed 9.6k times · Source

I'd like to have a base class that has a constant field (like an unique ID associated with the class that can't be modified after compile time). So far the static const declaration would be just fine. Now, I'd like to inherit this base class and make sure that the children of this class do have the same field, but with their own values. How can I do this?

Let's say, I'd like to have a base class called Base with an ID field that holds the int value of 0. Then, I'd like to have the classes A, B and C, all of them being public children of Base and I'd like to make sure that these children would also have the ID fields with the respective values of 1, 2 and 3 (by 'making sure', I mean something like getting a compiler error if they don't have a ID explicitly declared).

If I could manage to build this scenario, my expectation would be that asking for the ID field of a Base* pointer, I should get different values depending whether the pointer was created as new A(), new B() or new C().

My guess would be to declare ID as virtual static const, which of course doesn't make sense and gives a compiler error.

But then what can I do to achieve the described result? (The only thing that I could imagine would be to declare ID as a virtual function returning an integer and then hard-code the value into the function body, but I'm looking for something more elegant.)

Thank you in advance!

Answer

Luchian Grigore picture Luchian Grigore · Jun 6, 2012

A static method cannot be virtual, and no data members can be virtual.

But you can hide static fields in derived classes and use a virtual method to return them.

class A
{
public:
    static const int ID = 0;
    virtual int getID() { return A::ID; }
};
class B : A
{
public:
    static const int ID = 1;
    virtual int getID() { return B::ID; }
};

Alternative:

class A
{
public:
    A(int id = 0) : ID(id) {}
    const int ID;
    getID() { return ID; }
};
class B : public A
{
public:
    B() : A(1) {}
};