C++ – Why virtual & static keywords aren’t allowed outside class declaration

c++staticsyntaxvirtual

I am curious to know why following isn't allowed in C++?

1st program:

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

[Error] 'virtual' outside class declaration

2nd program:

#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; 
}

[Error] cannot call member function 'int Test::myfun()' without object

So, my questions are

Why can't I make a member function virtual like as in 1st program?

Why can't I make a member function static like as in 2nd program?

Is there any reason to not allow these 2 keywords outside the class?

Best Solution

The modifiers must be on the function declarations, otherwise it would be impossible to call the functions given just the declarations.

Since they must be on the declarations, it would be redundant to put them on the definitions as well. There's no particularly good reason to disallow them (as long as they match the declaration), but no particularly good reason to allow them either.