Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is function overriding done?

Where in the process of creating the program, compiler, linker etc., is the overriding of functions and operator overloading done?

I'm particularly interested where it is done in C++, Ruby and Python.

like image 732
Obto Avatar asked Jul 24 '11 21:07

Obto


People also ask

How is function overriding done?

Overriding occurs in a parent class and its child class. No special keyword is used to overload a function. Virtual keyword in the base class and Override keyword in the derived class can be used to override a function.

Is there function overriding in C?

Suppose, the same function is defined in both the derived class and the based class. Now if we call this function using the object of the derived class, the function of the derived class is executed. This is known as function overriding in C++. The function in derived class overrides the function in base class.

What is function overriding give example?

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.


2 Answers

Function overloading is (at least in C++) handled internally inside the compiler. The idea is that the code that the compiler ultimately generates will be hardcoded to call the appropriate function, as if the functions all had different names and you called the function uniquely suited to the arguments. More generally, in most compiled languages that support overloading, the overload resolution is done at compile-time and the emitted code will always call the indicated function. For example, Haskell supports compile-time overloading this way.

Operator overloading is a special case of general overloading, so it's usually handled the same way.

Function overriding (a term that arises in OOP when a derived class inherits from a base class and redefines one of its methods) is almost always resolved at runtime, since a compiler can't always tell which function is going to be invoked without actually knowing about the types at runtime. Some compilers might be able to statically prove that a certain object has a specific type and can then optimize the dynamic dispatch away, but it's impossible to do this in all cases.

I am not aware of any dynamic languages that support overloading, since in theory you could introduce new overload candidates as the program was running. I would love to be enlightened if such a language exists.

like image 52
templatetypedef Avatar answered Oct 23 '22 11:10

templatetypedef


For C++, operator overloading is done at the compiler level though a name-mangling process that creates a unique name identifier for every function so that the linker will not complain about duplicate function definitions. In C++, operator overloading is possible because overloadable operations like +, -, *, etc. are actual functions themselves that have the prefix operator followed by the symbol of the operation. So for instance, an overloaded operator+ function with a function signature like

my_type operator+(const my_type& lhs, const my_type& rhs);

will not conflict with another operator+ function with a different signature, even though both functions have the same operator+ name, because each version of the function will have a different name at the assembly-language level after the C++ compiler's name-mangling process is complete. Name-mangling has another benefit in that allows C and C++ compiled code to be used with the same linker, since two functions with the same name will not exist and cause a linker error.

Note that in C, that even if you create two functions with different signatures, if they have the same name, since the C-compiler will not do any name-mangling, the linker will complain about duplicate definitions of the function.

like image 41
Jason Avatar answered Oct 23 '22 09:10

Jason