方法重写和重载
我可以仅通过使用不同的返回值来重载或覆盖方法吗?在这种情况下是虚拟物质吗?
例如:
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 ?
关于超载:
class A{
virtual int Foo();
virtual float Foo(); // is this possible ?
int Goo();
float Goo(); // or maybe this ?
}
Class B{
double Foo();
}
谢谢
can i overload or override methods just by using different return value ? is virtual matter in thie case ?
for example :
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 ?
and regarding overloading :
class A{
virtual int Foo();
virtual float Foo(); // is this possible ?
int Goo();
float Goo(); // or maybe this ?
}
Class B{
double Foo();
}
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
函数的返回类型不是其签名的一部分,因此不能重载。
The return type of a function is not part of its signature, thus it can't be overloaded.
不,你不能那样做。
您可以在重写方法中执行的操作是返回指向某个类型的指针/引用,该类型是父级方法返回的类型的后代。
例子:
No, you cannot do that.
What you can do in an overriden method is to return a pointer/reference to a type which is a descendant of the type returned by parent's method.
Example:
返回值不是函数签名的一部分,编译器现在如何选择哪个版本?
如果您在第一个示例中从
Foo
中删除virtual
,它将起作用并调用Base::Foo
。The return value is not part of the function signature, how would the compiler now which version to chose?
If you remove
virtual
fromFoo
in your first example it will work and callBase::Foo
.仅更改返回值的重载是不可能的。
考虑一下:
在没有任何返回值分配的情况下调用,那么编译器无法推断出要调用哪个方法。
virtual
关键字用于创建VTable
,从而证明动态多态性。它对重载方法没有影响。Overloading with only change in the return value is not possible.
consider this:
called without any return value assigning then compiler cannot deduce which method to invoke.
virtual
keyword is used for creatingVTable
and thereby proving dynamic polymorphism. It has no impact on overloading methods.不幸的是,返回值不是签名的一部分,所以不是。
The return value is not part of the signature, unfortunetly, so no.
就覆盖而言,它与类雇佣体系有关,并且有可能实现运行时多态性。
根据 C++ 标准,跨范围重载是不可能的。
As far as overriding is concern it is pertaining to class hirearchy and it possible to achieve runtime polymorhism.
Overloading across scopes is not possible as per C++ standard.
不,您可以做一些类似的事情,例如模板化返回值(可能具有专门化)并在调用时指定模板类型,例如
No, there are some similar thing you can do however e.g. templatize the return value (possibly with specializations) and specify the template type when calling e.g.
如果您使用 VC++,您将收到“重载方法仅在返回类型上不同”类型的错误。这是因为方法/函数签名不包含返回类型。我猜这是因为如果返回值没有分配给任何东西,它可能会导致歧义。例如
int iMyvar = object.method(); // 显而易见应该返回什么类型
对比:-
object.method(); // 如果返回类型是签名的一部分,编译器将调用哪个重载?模棱两可。
You'll get a "overloaded method only differs in return type" type of error if you're using VC++. This is because method/function signatures don't incorporate the return type. I'm guessing it's because of the ambiguity it can cause if the return value isn't being assigned to anything. e.g
int iMyvar = object.method(); // obvious what type should be returned
Contrast with:-
object.method(); // which overload would the compiler call if return type was part of signature? Ambiguos.