Possible Duplicate:
C++ static virtual members?
我们可以有一个虚拟静态方法(在 C++ 中)吗?我试图编译以下 代码:
#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;
}
但是编译器说:
member 'f' cannot be declared both virtual and static
所以我猜答案是否定的,但为什么呢?
谢谢, 罗恩
最佳答案
没有。 static
在类中的函数上表示该函数不需要对象来操作。 virtual
表示实现取决于调用对象的类型。对于静态,没有调用对象,因此在同一个函数上同时拥有 static
和 virtual
没有意义
.
https://stackoverflow.com/questions/7227236/