Home >Backend Development >C++ >Can Static Functions Be Overloaded with Non-Static Functions in C ?
Overloading Static Functions with Non-Static Functions in C
Context:
Overloading functions with different return types is not supported in C , and the same applies when attempting to overload a static function with a non-static function. This scenario was illustrated in the provided class definition, but it yielded an error.
Standard Prohibition:
This behavior is explicitly prohibited by the C standard (ISO 14882:2003, Section 13.1/2):
Ambiguity Considerations:
Even if overloading was allowed, it would introduce ambiguity because:
For example:
<code class="cpp">class Foo { public: static void print() { cout << "static" << endl; } }; Foo f; f.print(); // Ambiguous: static or non-static call?</code>
Alternative Approach:
Since determining whether a function is called statically or not is not possible in C , alternative methods can be used to achieve the desired functionality:
The above is the detailed content of Can Static Functions Be Overloaded with Non-Static Functions in C ?. For more information, please follow other related articles on the PHP Chinese website!