Ron*_*n_s 41 c++ virtual static
可能重复:
C++静态虚拟成员?
我们可以使用虚拟静态方法(在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;
}
Run Code Online (Sandbox Code Playgroud)
但是编译器说:
member 'f' cannot be declared both virtual and static
Run Code Online (Sandbox Code Playgroud)
所以我猜答案是否定的,但为什么呢?
谢谢,罗恩
Mic*_*son 53
static对类中的函数的编号意味着该函数不需要操作对象.virtual表示实现取决于调用对象的类型.对于静态也没有调用对象,所以它没有任何意义有两个static与virtual上相同的功能.
| 归档时间: |
|
| 查看次数: |
49923 次 |
| 最近记录: |