Calling a derived-class implementation of a pure virtual function statically using a function in the base class

StackOverflow https://stackoverflow.com/questions/23431386

Question

There's some discussion of this topic elsewhere in stackoverflow, but I haven't really found a clear answer to my question.

My setup is like this:

class BaseClass
{
    virtual short return_number_for_thing1(std::string thing1)=0; //note the pure virtual
    virtual short return_number_for_thing2(std::string thing2)=0;
    virtual short return_number_for_thing(std::string thing); //virtual is probably not necessary here, I can get rid of it if need be
}

short BaseClass::return_number_for_thing(std::string thing)
{ //pretend thing1 and thing2 are enum'ed somewhere
    if      (thing == thing1) return return_number_for_thing1(thing);
    else if (thing == thing2) return return_number_for_thing2(thing);
}

class DerivedClass1 : BaseClass
{
    short return_number_for_thing1(std::string thing1);
    short return_number_for_thing2(std::string thing2);
}

class DerivedClass2 : BaseClass
{
    short return_number_for_thing1(std::string thing1);
    short return_number_for_thing2(std::string thing2);
}

My question is, why can't I write code like this:

short number_i_want = DerivedClass2::return_number_for_thing(thing);

I sort of understand that trying to call return_number_for_thing from a BaseClass pointer doesn't make sense, since it doesn't know whether to call the routines for DerivedClass1 or DerivedClass2, but if I give it the scope of DerivedClass2, shouldn't it be able to figure out what I want? For now, I create a blank instance of DerivedClass2 or DerivedClass1 when I need to, but it seems to me that I shouldn't have to do that.

Was it helpful?

Solution

In C++, virtual and static don't mix.

  • virtual = concrete operation depends on the type of an object.
  • static = you don't need an object.

It is certainly possible to imagine such a thing, however. If C++ had something like meta types, allowing you to treat regular types as objects, then it would not be such a strange idea anymore.

Pseudo-code (using an imaginary syntax):

void f(Class base_class)
{
   base_class.StaticMethod();
}

struct Base
{
  virtual static StaticMethod(); // impossible in C++
};

struct Derived : Base
{
  virtual static StaticMethod(); // impossible in C++
};

f(Base); // impossible in C++
f(Derived); // impossible in C++

The desire to create something like static virtual functions is sometimes a symptom for the real need (which C++ cannot fulfill out of the box): treating types as objects.

OTHER TIPS

You can make a function virtual or static , not both.

A virtual function must have a vtable pointer which requires an instance of an object, a static function cannot (by definition) have an instance to which it relates.

If you declare the functions as e.g:

virtual short return_number_for_thing1(std::string thing1)=0;

then you must create an instance and use it e.g. m_Instance->return_number_for_thing1(...) (but since you are not using any member variables it would seem this doesnt really make sense).

If you declare the functions as static:

static short return_number_for_thing1(std::string thing1)

Then you must invoke the function statically BaseClass::return_number_for_thing1(), if you provide a static version of the same function in the derived class it now becomes an override and you choose which one you call at compile time with either BaseClass::return_number_for_thing1 or DerivedClass::return_number_for_thing1..

Your question still doesnt really make sense as stated since you cannot use a virtual function statically. Statically implies that you want to use it without any instance , its simply like a c function but hidden in the namespace of the class (and obeys privacy). Virtual functions require an instance on which to operate.

Making static virtual methods isn't possible, quoting C++11 standard N3337 - 10.3.10:

[ Note: The virtual specifier implies membership, so a virtual function cannot be a nonmember (7.1.2) function. Nor can a virtual function be a static member, since a virtual function call relies on a specific object for determining which function to invoke. A virtual function declared in one class can be declared a friend in another class. —end note ]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top