我们都知道你可以根据参数重载一个函数:
int mul(int i, int j) { return i*j; }
std::string mul(char c, int n) { return std::string(n, c); }
Run Code Online (Sandbox Code Playgroud)
你能根据返回值重载一个函数吗?根据返回值的使用方式定义一个返回不同内容的函数:
int n = mul(6, 3); // n = 18
std::string s = mul(6, 3); // s = "666"
// Note that both invocations take the exact same parameters (same types)
Run Code Online (Sandbox Code Playgroud)
您可以假设第一个参数介于0-9之间,无需验证输入或进行任何错误处理.
我可以通过使用不同的返回值来重载或覆盖方法吗?在这种情况下是虚拟的事情?
例如 :
class A{
virtual int Foo(); // is this scenario possible with/without the keyword virtual
}
class B : public A {
virtual double Foo();
}
A* Base = new B();
int i = Base->Foo(); // will this just convert double to int ?
Run Code Online (Sandbox Code Playgroud)
并且有关重载:
class A{
virtual int Foo();
virtual float Foo(); // is this possible ?
int Goo();
float Goo(); // or maybe this ?
}
Class B{
double Foo();
}
Run Code Online (Sandbox Code Playgroud)
谢谢