However, the C++ compiler does not take into account the presence or absence of the static keyword when distinguishing functions, but only looks at the function name, return type and parameter list. void foo(); and static void foo(); are considered duplicates. Declaration is prohibited, which means that there can only be one void foo(); function in a class, regardless of whether it is static or non-static. Why?
Then imagine that class A has another function void bar(); defined as follows:
void A::bar(){
//....
foo();
//....
}
This way of writing is almost a convention. When calling function foo() within function bar(), you write foo() directly; instead of writing the static, const and other keywords used in function declaration, In this way, if you have a void foo() and a staic void foo(), the compiler cannot distinguish which foo() you are calling;
Since there can only be one void foo(), there is no need to write static again when you define it. The compiler already knows which function it is.
When static is used to modify member variables or member functions, it can only be declared inside the class when it is declared. There is no need to add static when instantiating or defining it.
Imagine if you have two foo() functions in class A:
In order to distinguish them, you only need to write static when defining:
However, the C++ compiler does not take into account the presence or absence of the static keyword when distinguishing functions, but only looks at the function name, return type and parameter list. void foo(); and static void foo(); are considered duplicates. Declaration is prohibited, which means that there can only be one void foo(); function in a class, regardless of whether it is static or non-static. Why?
Then imagine that class A has another function void bar(); defined as follows:
This way of writing is almost a convention. When calling function foo() within function bar(), you write foo() directly; instead of writing the static, const and other keywords used in function declaration, In this way, if you have a void foo() and a staic void foo(), the compiler cannot distinguish which foo() you are calling;
Since there can only be one void foo(), there is no need to write static again when you define it. The compiler already knows which function it is.
When static is used to modify member variables or member functions, it can only be declared inside the class when it is declared. There is no need to add static when instantiating or defining it.