C ++中的重写和重载 [英] Override and overload in C++

查看:255
本文介绍了C ++中的重写和重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的,我确实了解它们之间的区别.我想知道的是:为什么要覆盖一种方法?这样做有什么好处? 如果发生重载:唯一的优势就是您不必为函数使用不同的名称?

Yes, I do understand the difference between them. What I want to know is: why OVERRIDE a method? What is the good in doing it? In case of overload: the only advantage is you haven't to think in different names to functions?

推荐答案

重载通常意味着您在同一个作用域中有两个或两个以上具有相同名称的函数.进行调用时,与参数更好地匹配的函数将获胜并被调用.与调用虚拟函数相反,需要注意的重要一点是,在编译时就选择了要调用的函数.这完全取决于参数的静态类型.如果B有一个重载,而D有一个重载,并且该参数是对B的引用,但它确实指向D对象,则在C ++中选择B的重载.这称为静态调度,而不是动态调度.如果要与具有相同名称的另一个函数执行相同的操作,则需要重载,但是要为另一个参数类型执行此操作.示例:

Overloading generally means that you have two or more functions in the same scope having the same name. The function that better matches the arguments when a call is made wins and is called. Important to note, as opposed to calling a virtual function, is that the function that's called is selected at compile time. It all depends on the static type of the argument. If you have an overload for B and one for D, and the argument is a reference to B, but it really points to a D object, then the overload for B is chosen in C++. That's called static dispatch as opposed to dynamic dispatch. You overload if you want to do the same as another function having the same name, but you want to do that for another argument type. Example:

void print(Foo const& f) {
    // print a foo
}

void print(Bar const& bar) {
    // print a bar
}

它们都显示其参数,因此它们很重载.但是第一个打印一个foo,第二个打印一个bar.如果您有两个执行不同功能的函数,则当它们具有相同的名称时,这将被认为是较差的样式,因为这可能导致人们对调用函数时实际发生的事情感到困惑.重载的另一个用例是当您具有其他功能参数时,它们只是将控制权转发给其他功能:

they both print their argument, so they are overloaded. But the first prints a foo, and the second prints a bar. If you have two functions that do different things, it's considered bad style when they have the same name, because that can lead to confusion about what will happen actually when calling the functions. Another usecase for overloading is when you have additional parameters for functions, but they just forward control to other functions:

void print(Foo & f, PrintAttributes b) { 
    /* ... */ 
}

void print(Foo & f, std::string const& header, bool printBold) {
    print(f, PrintAttributes(header, printBold));
}

如果经常使用重载所采用的选项,那么这对于调用者可能很方便.

That can be convenient for the caller, if the options that the overloads take are often used.

覆盖完全不同.它不能与过载竞争.这意味着,如果您在基类中具有虚函数,则可以在派生类中编写具有相同签名的函数.派生类中的功能覆盖基类的功能.样本:

Overriding is something completely different. It doesn't compete with overloading. It means that if you have a virtual function in a base class, you can write a function with the same signature in the derived class. The function in the derived class overrides the function of the base. Sample:

struct base {
    virtual void print() { cout << "base!"; }
}

struct derived: base {
    virtual void print() { cout << "derived!"; }
}

现在,如果您有一个对象并调用print成员函数,则将始终调用派生的打印函数,因为它会覆盖基数之一.如果函数print不是虚函数,则派生函数将不会覆盖基本函数,而只会隐藏.如果您有一个接受基类的函数以及从中派生的每个基类,则重写可能很有用:

Now, if you have an object and call the print member function, the print function of the derived is always called, because it overrides the one of the base. If the function print wasn't virtual, then the function in the derived wouldn't override the base function, but would merely hide it. Overriding can be useful if you have a function that accepts a base class, and every one that's derived from it:

void doit(base &b) {
    // and sometimes, we want to print it
    b.print();
}

现在,即使在编译时编译器仅知道b至少是基数,也将调用派生类的打印.这就是虚函数的关键所在.没有它们,将调用基类的print函数,并且派生类中的那个函数不会覆盖它.

Now, even though at compile time the compiler only knows that b is at least base, print of the derived class will be called. That's the point of virtual functions. Without them, the print function of the base would be called, and the one in the derived class wouldn't override it.

这篇关于C ++中的重写和重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆