Where is function overriding done?

By : Obto
Source: Stackoverflow.com

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.

By : Obto


Python is not linked/compiled, it is interpreted. So, the normal overriding is done when class sources are parsed. Of course, due to dynamic nature you can always override during the runtime as well. I suppose that alternate implementations using the byto-code compilation do it on the compile-time.

I also think the above is true for Ruby as well.

By : Guard

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
By : Jason

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.

This video can help you solving your question :)
By: admin