C++ Overriding… overwriting?

。_饼干妹妹 提交于 2019-12-17 23:18:37

问题


I know what overriding is in C++. But, is there overwriting? If so, what does it mean?

Thanks.


回答1:


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 of x).




回答2:


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?




回答3:


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.




回答4:


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() overrides fruit::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:

<script type="text/javascript" 
    src="some super big framework, often in one big file">
<script type="text/javascript">
  Ext.ux.window.createWin = function() {
     // completely OVERWRITE the implementation 
       (often to 'hotfix' a particular bug)
  }
</script>

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.



来源:https://stackoverflow.com/questions/4738315/c-overriding-overwriting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!