C++ 中的运算符重载和运算符覆盖有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3803970/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between operator overloading and operator overriding in C++?
提问by bunty
What is the main difference between operator overloadingand operator overridingin C++?
C++ 中运算符重载和运算符覆盖的主要区别是什么?
回答by Johannes Schaub - litb
Some usethe latter term to describe what's being done when you defined an own global operator new
or operator delete
. That's because your own definition can replacethe default version in the library. The C++ Standard uses the words replacesand displacesfor this. Using "override" is a bit confusing because that term is already used for virtual functions being overridden by a function in a derived class.
有些人使用后一个术语来描述当您定义自己的全局operator new
或operator delete
. 那是因为您自己的定义可以替换库中的默认版本。C++ 标准为此使用了替换和置换这两个词。使用“覆盖”有点令人困惑,因为该术语已经用于被派生类中的函数覆盖的虚函数。
The term "overloading" is the general term used for defining your own operator functions. This term is used even if no actual overloading happens by the operator function. One way to approach this term is because it "overloads" the built-in meaning of certain operators.
术语“重载”是用于定义您自己的运算符函数的通用术语。即使操作符函数没有发生实际重载,也会使用该术语。处理这个术语的一种方法是因为它“重载”了某些运算符的内置含义。
回答by Delan Azabani
I've never heard the latter term, though I'd assume it'd be the same as the first one. Operator overloadingis where you provide a function for a class to behave when used in an operator context. For example, if I had a class point
, and wanted to add them such as a + b
, I'd have to create an operator+(point other)
function to handle this.
我从未听说过后一个术语,尽管我认为它与第一个相同。运算符重载是您为类在运算符上下文中使用时的行为提供函数的地方。例如,如果我有一个 class point
,并且想要添加它们,例如a + b
,我必须创建一个operator+(point other)
函数来处理这个问题。
回答by David Rodríguez - dribeas
In general overloadingis used to identify when there is more than one signature for a given function name. Each such defined function is an overloadof the function name. On the other hand overrideis present only in polymorphic (virtual
in C++) member functions, where a redefinition of the same signaturein a derived method overridesthe behavior provided in the base class.
通常,重载用于识别给定函数名称何时有多个签名。每个这样定义的函数都是函数名的重载。另一方面,override仅存在于多态(virtual
在 C++ 中)成员函数中,其中派生方法中相同签名的重新定义会覆盖基类中提供的行为。
struct base {
virtual void foo();
void foo(int); // overloads void foo()
};
struct derived : base {
void foo(); // overrides void base::foo()
void foo(int); // overloads void derived::foo()
// unrelated to void::base(int)
};
int main() {
derived d;
base & b = d;
b.foo( 5 ); // calls void base::foo(int)
b.foo(); // calls the final overrider for void base::foo()
// in this particular case void derived::foo()
d.foo( 5 ); // calls void derived::foo(int)
}
回答by Woot4Moo
Nice homework question. I'll help you here... Overloading means 2 methods with the SAME Name and different signatures + return types. Overriding means 2 methods with the SAME name, wherein the sub method has different functionality. Examples fall on you for this exercise.
很好的家庭作业问题。我会在这里帮助你...重载意味着 2 个方法具有相同的名称和不同的签名 + 返回类型。覆盖意味着具有相同名称的 2 个方法,其中子方法具有不同的功能。这个练习的例子落在了你身上。
回答by Ashish Yadav
if the question is regarding function overloading and function overriding then... @woot4moo has explained that thing . As far as my knowledge is concerned i have never heard of "operator overriding". operator overloading is done to provide your class's object with special functionality related to that operator , whenever that operator is to be used with the object . For example a class should provide an operator(=) to always prevent shallow coping.(well that is done in conjunction with a copy constructor).
如果问题是关于函数重载和函数覆盖那么......@woot4moo 已经解释了那件事。就我所知,我从未听说过“操作员覆盖”。运算符重载的目的是为您的类的对象提供与该运算符相关的特殊功能,只要该运算符与对象一起使用。例如,一个类应该提供一个 operator(=) 以始终防止浅层复制。(与复制构造函数一起完成)。
回答by bunty
The main difference between overloadingand overridingis that in overloading we can use same function name with different parameters for multiple times for different tasks with on a class. and overridingmeans we can use same name function name with same parameters of the base class in the derived class. this is also called as re useability of code in the programme.
重载和覆盖之间的主要区别在于,在重载中,我们可以为一个类的不同任务多次使用具有不同参数的相同函数名称。和重写装置,我们可以使用相同的名称的函数名与在派生类的基类的参数相同。这也称为程序中代码的可重用性。
回答by Ayush
If derive class define same function as base class then it is known as overriding. When name of function is same but different arguments are use to difine the function are overloading.
如果派生类定义与基类相同的功能,则称为覆盖。当函数名相同但使用不同的参数来区分时,函数正在重载。
回答by Ayush
I guess "operator overriding" applies when you declare an operator inside a class and make it virtual. That is a very fragile thing to do - you're usually better off doing something else.
我猜当您在类中声明一个运算符并使其成为虚拟时,“运算符覆盖”适用。这是一件非常脆弱的事情——你通常最好做些别的事情。