46

Possible Duplicate:
C++ static virtual members?

Can we have a virtual static method (in C++) ? I've tried to compile the following code :

#include <iostream>
using namespace std;

class A
{
public:
    virtual static void f() {cout << "A's static method" << endl;}
};

class B :public A
{
public:
    static void f() {cout << "B's static method" << endl;}
};

int main()
{
    /* some code */
    return 0;
}

but the compiler says that :

member 'f' cannot be declared both virtual and static

so I guess the answer is no , but why ?

thanks , Ron

Community
  • 1
  • 1
Ron_s
  • 1,429
  • 1
  • 14
  • 24
  • 1
    There is no real reason. It is just not supported in C++. (It works in Python: https://ideone.com/fWtTUi) - You can create a virtual wrapper around a static method: A virtual method that ignores the object and forwards the call to the static method. – not-a-user Mar 16 '18 at 11:25

4 Answers4

60

No. static on a function in a class means that the function doesn't need an object to operate on. virtual means the implementation depends on the type of the calling object. For static there is no calling object, so it doesn't make sense to have both static and virtual on the same function .

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • 5
    Your answer is correct, but your explanation is not. You could use an object to determine which static method to call. In Python it works like that: https://ideone.com/fWtTUi – not-a-user Mar 16 '18 at 11:15
  • 2
    @not-a-user You can also call a static function from an object in C++, so you can do `f.static_function()` as well as 'F::static_function()` (I said doesn't need an object, not cant be called on). But you're right - in the case where we have an object C++ could use the vtable to look up an appropriate function - in which case virtual static would make sense. And as you say overloading could be used to forward a virtual call to the correct static function (or does the compiler consider that ambiguous?) – Michael Anderson Mar 16 '18 at 13:28
3

Don't think this is possible because you could call A::F(); without having the object A. Making it virtual and static would mean a contradiction.

RvdK
  • 19,580
  • 4
  • 64
  • 107
  • 3
    Aha , then static means that *this doesn't exists , virtual means that the method has a this pointer . The combining of the two makes a contradiction . Nice :) thanks – Ron_s Aug 29 '11 at 07:29
  • 1
    No, there is no contradiction. In `A::F()` the class is hardcoded, while in `this->F()` the right function to call could be derived from the object (via virtual function lookup). The fact is that C++ has no support for this. In Python it works out of the box: https://ideone.com/fWtTUi – not-a-user Mar 16 '18 at 11:21
2

No, static function is like global function, but also inside class namespace. virtual implies inheritance and reimplementing in derived class - you can't reimplement 'global' function.

ks1322
  • 33,961
  • 14
  • 109
  • 164
1

Because the class doesn't have a this pointer. In there is the virtual function lookup table. A quick google can tell you more about the virtual function lookup table.

EnabrenTane
  • 7,428
  • 2
  • 26
  • 44