C++覆盖...覆盖?

发布于 2024-10-13 00:21:49 字数 90 浏览 6 评论 0原文

我知道 C++ 中的重写是什么。但是,有覆盖吗?如果是这样,这意味着什么?

谢谢。

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

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

强者自强 2024-10-20 00:21:49

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

暖树树初阳… 2024-10-20 00:21:49

您可以覆盖变量,例如 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?

花落人断肠 2024-10-20 00:21:49

我所熟悉的通常区别是重写和重载。
虚函数被重写。当存在具有相同名称但不同签名的版本时,函数会重载(这在许多语言中都存在)。在 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.

彼岸花似海 2024-10-20 00:21:49

重写在OOP中是“正常的事情”:派生类为某些东西提供不同的(即更专门的)实现,重写基类,例如apple::foo() 如果 apple 是派生自 Fruit 的类,则覆盖 fruit::foo()。 (不要通过使用不同的参数签名来误认为重载,这会导致完全不同的功能)。

覆盖我知道完全替换为另一个定义。不是针对特定级别,而是针对该计划的其余部分。如果一个大框架有一些特殊问题,并且您不想将大文件分开,有时会使用 javascript:

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

但是:我不知道 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() 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.

Hello爱情风 2024-10-20 00:21:49

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.

小巷里的女流氓 2024-10-20 00:21:49

重写是指在基类中使用virtual关键字创建一个方法,并且基类允许子类使用
为自己制作一个相同方法的主体。

覆盖的意思是覆盖而不使用虚拟关键字。

重载是指用一个名称创建多个具有不同输入参数的方法。

样本:

    #include <stdio.h>
    #include <stdlib.h>
    class par_overwrite
    {public:    par_overwrite() {}; ~par_overwrite() {};
        int at() { return 1; };
    };
    class chld_overwrite :public par_overwrite
    {public:    chld_overwrite() {};    ~chld_overwrite() {};
        int at() { return 2; }//overwrite
    };
    void main_overwrite()
    {
        par_overwrite pt;
        int ptat = pt.at();
        chld_overwrite ct;
        int ctat = ct.at();
        printf("ptat:%d,ctat:%d\n",ptat, ctat);  //output : ptat:1,ctat:2
    }
    class par_override
    {public:    par_override() {};  ~par_override() {};
        virtual int ad() { return 3; };
    };
    class chld_override :public par_override
    {public:    chld_override() {}; ~chld_override() {};
        int ad() { return 4; }//override
    };
    void main_override()
    {
        par_override pd;
        int pdad = pd.ad();
        chld_override cd;
        int cdad = cd.ad();
        printf("pdad:%d,cdad:%d\n", pdad, cdad); //output : pdad:3,cdad:4
    }
    class par_override_pure
    {public:    par_override_pure() {}; ~par_override_pure() {};
        virtual int adp()=0;//Pure Virtual Function
    };
    class chld_override_pure :public par_override_pure
    {public:    chld_override_pure() {};    ~chld_override_pure() {};
        int adp() { return 5; }//override from Pure Virtual Function
    };
    void main_override_pure()
    {
        //par_override_pure pdp;//error : Can not create object from abstract class that have (Pure Virtual Function)
        //int pdpad = pdp.ad();//error
        chld_override_pure cdp;
        int cdpadp = cdp.adp();
        printf("cdpadp:%d\n", cdpadp); //output : cdpadp:5
    }
    class overload
    {public:    overload() {}; ~overload() {};
        void method_overload(int prm1) { printf("ol1\t"); }
        void method_overload(int prm1, int prm2) { printf("ol2\t"); }
        void method_overload(int prm1, int prm2, int prm3) { printf("ol3\t"); }
        void method_overload(double prm1) { printf("ol4\t"); }
        void method_overload(double prm1, double prm2) { printf("ol5\t"); }
        void method_overload(double prm1, double prm2, double prm3) { printf("ol6\t"); }
    };
    void main_overload()
    {
        overload ol;
        ol.method_overload(1);
        ol.method_overload(1, 1);
        ol.method_overload(1, 1, 1);
        ol.method_overload(0.1);
        ol.method_overload(0.1, 0.1);
        ol.method_overload(0.1, 0.1, 0.1); // output : ol1     ol2     ol3     ol4     ol5     ol6
    }
    int main()
    {   main_overwrite();
        main_override();
        main_override_pure();
        main_overload();

        getchar();
        return 0;
    }
    /* output:
    ptat:1,ctat:2
    pdad:3,cdad:4
    cdpadp:5
    ol1     ol2     ol3     ol4     ol5     ol6
    */

Override is meaning making a method with virtual keyword in the base class and the base class is allowing to the child classes
for make a body of same method for it self .

Overwrite is meaning Override without virtual keyword.

Overload is meaning make multiple methods with different input parameters with One name.

SAMPLES:

    #include <stdio.h>
    #include <stdlib.h>
    class par_overwrite
    {public:    par_overwrite() {}; ~par_overwrite() {};
        int at() { return 1; };
    };
    class chld_overwrite :public par_overwrite
    {public:    chld_overwrite() {};    ~chld_overwrite() {};
        int at() { return 2; }//overwrite
    };
    void main_overwrite()
    {
        par_overwrite pt;
        int ptat = pt.at();
        chld_overwrite ct;
        int ctat = ct.at();
        printf("ptat:%d,ctat:%d\n",ptat, ctat);  //output : ptat:1,ctat:2
    }
    class par_override
    {public:    par_override() {};  ~par_override() {};
        virtual int ad() { return 3; };
    };
    class chld_override :public par_override
    {public:    chld_override() {}; ~chld_override() {};
        int ad() { return 4; }//override
    };
    void main_override()
    {
        par_override pd;
        int pdad = pd.ad();
        chld_override cd;
        int cdad = cd.ad();
        printf("pdad:%d,cdad:%d\n", pdad, cdad); //output : pdad:3,cdad:4
    }
    class par_override_pure
    {public:    par_override_pure() {}; ~par_override_pure() {};
        virtual int adp()=0;//Pure Virtual Function
    };
    class chld_override_pure :public par_override_pure
    {public:    chld_override_pure() {};    ~chld_override_pure() {};
        int adp() { return 5; }//override from Pure Virtual Function
    };
    void main_override_pure()
    {
        //par_override_pure pdp;//error : Can not create object from abstract class that have (Pure Virtual Function)
        //int pdpad = pdp.ad();//error
        chld_override_pure cdp;
        int cdpadp = cdp.adp();
        printf("cdpadp:%d\n", cdpadp); //output : cdpadp:5
    }
    class overload
    {public:    overload() {}; ~overload() {};
        void method_overload(int prm1) { printf("ol1\t"); }
        void method_overload(int prm1, int prm2) { printf("ol2\t"); }
        void method_overload(int prm1, int prm2, int prm3) { printf("ol3\t"); }
        void method_overload(double prm1) { printf("ol4\t"); }
        void method_overload(double prm1, double prm2) { printf("ol5\t"); }
        void method_overload(double prm1, double prm2, double prm3) { printf("ol6\t"); }
    };
    void main_overload()
    {
        overload ol;
        ol.method_overload(1);
        ol.method_overload(1, 1);
        ol.method_overload(1, 1, 1);
        ol.method_overload(0.1);
        ol.method_overload(0.1, 0.1);
        ol.method_overload(0.1, 0.1, 0.1); // output : ol1     ol2     ol3     ol4     ol5     ol6
    }
    int main()
    {   main_overwrite();
        main_override();
        main_override_pure();
        main_overload();

        getchar();
        return 0;
    }
    /* output:
    ptat:1,ctat:2
    pdad:3,cdad:4
    cdpadp:5
    ol1     ol2     ol3     ol4     ol5     ol6
    */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文