开发者

What is the difference between operator overloading and operator overriding in C++?

开发者_Go百科What is the main difference between operator overloading and operator overriding in C++?


Some use the 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 replace the default version in the library. The C++ Standard uses the words replaces and displaces for 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.

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.


I've never heard the latter term, though I'd assume it'd be the same as the first one. Operator overloading is 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.


In general overloading is used to identify when there is more than one signature for a given function name. Each such defined function is an overload of the function name. On the other hand override is present only in polymorphic (virtual in C++) member functions, where a redefinition of the same signature in a derived method overrides the behavior provided in the base class.

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


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.


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


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.


The main difference between overloading and overriding is that in overloading we can use same function name with different parameters for multiple times for different tasks with on a class. and overriding means 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜