C++ Overriding… overwriting?

后端 未结 5 1558
情深已故
情深已故 2020-12-14 19:51

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

Thanks.

相关标签:
5条回答
  • 2020-12-14 20:16

    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.

    0 讨论(0)
  • 2020-12-14 20:22

    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.

    0 讨论(0)
  • 2020-12-14 20:36

    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.

    0 讨论(0)
  • 2020-12-14 20:37

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

    0 讨论(0)
  • 2020-12-14 20:43

    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?

    0 讨论(0)
提交回复
热议问题