为什么在类声明之外不允许使用虚拟和静态关键字?

Des*_*tor 0 c++ syntax virtual static

我很想知道为什么C++中不允许使用以下内容?

第一个项目:

#include <iostream>
class Test {
    public:
        int myfun();
}
virtual int Test::myfun()
{ return 0; }
int main()
{ }
Run Code Online (Sandbox Code Playgroud)

[错误]'虚拟'外部类声明

第二个项目:

#include <iostream>
class Test {
    public:
        int myfun();
};
static int myfun() {
    std::cout<<"This program contains an error\n";
    return 0; 
}
int main() {
  Test::myfun(); 
  return 0; 
}
Run Code Online (Sandbox Code Playgroud)

[Error]无法在没有对象的情况下调用成员函数'int Test :: myfun()'

所以,我的问题是

为什么我不能像第一个程序那样使成员函数虚拟化?

为什么我不能像第二个程序那样使成员函数静态化?

有没有理由在课外不允许这两个关键词?

Mik*_*our 6

修饰符必须在函数声明上,否则只能在声明时调用函数.

由于它们必须在声明上,因此将它们放在定义上也是多余的.没有特别好的理由拒绝它们(只要它们符合声明),但也没有特别好的理由允许它们.