Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between redefining and overriding a function

Suppose I have class A with a virtual function F():

class A {     virtual void F()     {         // Do something     }; }; 

And I have another class B which inherits A and redefines F():

class B : A {     void F()     {         // Do something     }; }; 

And a different class C which also inherits A but overrides F():

class C : A {     void F() override     {         // Do something     }; }; 

What is the difference between F() in classes B and C?

like image 591
Karnivaurus Avatar asked Feb 19 '16 13:02

Karnivaurus


People also ask

What is the difference between redefining and overriding a method in Java?

Redefining and Overriding comes with in the same scenarios. Only difference is that if methods used are Static, its redefining. Note: Static methods looks as if they are over-rided but they are actually redefined. Redefining is with Static Methods.

What is the difference between redefining a class function and overriding a base class function?

What's the difference between redefining a base class function and overriding a base class function? Overriding refers to the situation where a derived class redefines a virtual function. Overridden functions are dynamically bound, redefined functions are statically bound.

What is the difference between overloading a function and redefining a function?

redefining a function in a friend class is called function overloading while redefining a function in a derived class is called as overridden function. Answer» b. redefining a function in a friend class is called function overriding while redefining a function in a derived class is called an overloaded function.

What does it mean to redefine a function in C++?

A redefined function is a method in a descendant class that has a different definition than a non-virtual function in an ancestor class.


2 Answers

Both are overrides.

When you use the keyword override you ensure a compilation failure if it should happen to not be an override.

And that's good practice.

like image 190
Cheers and hth. - Alf Avatar answered Oct 17 '22 05:10

Cheers and hth. - Alf


Both B::f() and C::f() are overrides and they are exactly the same.

override is essentially a compile-time advisory term that will cause a compilation error if the function does not override one in a base class.

This can help program stability: if the name and parameter types to A::f() are changed, then a compile error will result.

like image 29
Bathsheba Avatar answered Oct 17 '22 05:10

Bathsheba