C++覆盖...覆盖?
我知道 C++ 中的重写是什么。但是,有覆盖吗?如果是这样,这意味着什么?
谢谢。
I know what overriding is in C++. But, is there overwriting? If so, what does it mean?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 C++ 术语中,有重写(与类层次结构中的虚拟方法相关)和重载(与具有相同名称但采用不同参数的函数相关)。您还可以隐藏名称(通过在嵌套声明区域或范围中显式声明相同名称)。
C++ 标准不使用术语“覆盖”,除非其规范的英语形式(即用新值替换一个值,如赋值
x = 10
中所示,该赋值覆盖了之前的值)x
)。In C++ terminology, you have overriding (relating to virtual methods in a class hierarchy) and overloading (related to a function having the same name but taking different parameters). You also have hiding of names (via explicit declaration of the same name in a nested declarative region or scope).
The C++ standard does not use the term "overwrite" except in its canonical English form (that is, to replace one value with a new value, as in the assignment
x = 10
which overwrites the previous value ofx
).您可以覆盖变量,例如 int a = 0; a = 42; 和文件(打开现有文件进行写入 - 如果您有权限,它将覆盖现有文件内容)如果这就是您的意思。这与覆盖无关。您是否考虑过超载?
You can overwrite variables, e.g.
int a = 0; a = 42;
and files (open an existing file for write - if you have permission it will overwrite the existing file contents) if that's what you mean. This has little in relation to overriding. Were you perhaps thinking of overloading?我所熟悉的通常区别是重写和重载。
虚函数被重写。当存在具有相同名称但不同签名的版本时,函数会重载(这在许多语言中都存在)。在 C++ 中,您还可以重载运算符。
AFAIK,覆盖是一个不相关的概念(覆盖变量、文件、缓冲区等),并且不是特定于 C++ 甚至 OOP 语言的。
The usual distinction I'm familiar with is of overriding and overloading.
Virtual functions are overridden. Functions are overloaded when there's a version with same name but different signature (this exists in many languages). In C++ you can also overload operators.
AFAIK, overwriting is an unrelated concept (overwrite a variable, file, buffer, etc.), and is not specific to C++ or even OOP languages.
重写在OOP中是“正常的事情”:派生类为某些东西提供不同的(即更专门的)实现,重写基类,例如
apple::foo()
如果 apple 是派生自 Fruit 的类,则覆盖fruit::foo()
。 (不要通过使用不同的参数签名来误认为重载,这会导致完全不同的功能)。覆盖我知道完全替换为另一个定义。不是针对特定级别,而是针对该计划的其余部分。如果一个大框架有一些特殊问题,并且您不想将大文件分开,有时会使用 javascript:
但是:我不知道 C++ 中有任何这样的事情,因为同时重新定义函数会总是在编译时就已经导致错误。最多,我可以想象弯曲函数指针,或(重新)定义回调挂钩。
Override is "the normal thing" in OOP: A derived class provides a different (i.e. more specialized) implementation for something, overriding the base class, e.g.
apple::foo()
overridesfruit::foo()
if apple is a class derived from fruit. (not to be mistaken with overload by using different parameter signatures, which leads to completely distinct functions).Overwrite I know as to completely replace with another-definition. Not on a specific level but in general for the remainder of the programm. This sometimes gets used javascript, if a big framework has some special issues, and you don't want to tear the big file apart:
However: I don't know of any such thing in C++, as a concurring redefinition of a function would always lead to errors already at compile time. At most, I can imaginge bending function pointers, or (re)defining call back hooks.
C++ 函数重写。如果派生类定义了与其基类中定义的相同的函数,则在 C++ 中称为函数重写。它用于实现运行时多态性。它使您能够提供其基类已提供的功能的具体实现。
C++ Function Overriding. If derived class defines same function as defined in its base class, it is known as function overriding in C++. It is used to achieve runtime polymorphism. It enables you to provide specific implementation of the function which is already provided by its base class.