Function Overloading In C++.

Function Overloading In C++.


Objectives:

  • Understanding What is meant by overloading in C++.
  • How does C++ deals with overloading under the hood.
  • Problem arises when using overloading.
  • Understand NULL and nullptr and their relation with overloading.

Understanding What is meant by overloading in C++.

Overloading in C++ happened when two or more function has the same name but different signature.

Signature of a function = function name + arguments types + # of arguments.

// Example: let's assume that we have the following two prototypes.


void print(int _x); // signature of this function print_int__x
void print(string _s); // signature of this function print_const_char__s.                                          
How does C++ deals with overloaded functions ?
// back to our example above if you take attention to the comments, we can see that c++ interpret the function with the following schema, 
[function name]_[data type of first argument]_.... and so on.        

  • If we look carefully in the assembly output for those two functions we would like see something like that:

// for void print(int _x) .
_[print][i], @function //print is function name, i (Integer datatype).
// for void print(string _s).
_[print]_[string] // print is function name, string data type for _s argument.        
Problem arises when using overloading.
// imagine that we have the following two functions definitions.


void print(int _x, int _y)
{
  cout << "integer = " << _x + _y << endl;
}


void print(float _f1, float _f2)
{
  cout << "float = " << _f1 + _f2 << endl;
}


int main ()
{
   int x = 10;
   float y = 20.f;
   // now call print function.
  print(x, y); // what would you expect in this case ? try it ! Do you think c++ will support you on this ?        
NULL and nullptr

One of the common questions is that why c++ introduces nullptr ?

  • To solve the ambiguities that happened in the last example.

// null pointer: pointer point to nothing.
#define NULL (0) 




// let's assume the following example.
bool isNull(int _x);// version #1
bool isNull(int* _x);// version #2.


// regardless the definitions for those functions let's use them directly.


int main ()
{
  int * _p  = NULL;
  
 // lets make a call to isNull.
 isNull(_p);//what would you expect in this case ? c++ will calll #1 or #2 ?          

  • The above code will run version #1 because NULL is defined as (0), in c++ 11 there is a new data type called nullptr that can be used in such a case instead of NULL.

https://stackoverflow.com/questions/20509734/null-vs-nullptr-why-was-it-replaced

Ahmed Elameer

Senior Functional Safety @ eJad | ISO26262 | Embedded Systems

2 年

???? ????? ????? ?? ???? ??

要查看或添加评论,请登录

Hazem Khaled的更多文章

  • Practicing on VSOMEIP

    Practicing on VSOMEIP

    To practise on VSOMEIP you need the following setup: Host Machine: Linux Machine use "Ubuntu" OS. Virtual Machine on…

  • Next: How SOMEIP and SOMEIP-SD Work

    Next: How SOMEIP and SOMEIP-SD Work

    SOMEIP and SOMEIP-SD developed by BMW Group at 2011, to over come the issue of "static data path" and "static data…

  • Look Inside ECU

    Look Inside ECU

    Before going to Service-Oriented Architecture (SOA) and the SOME/IP protocol, that enabled the "Dynamic Path and…

  • Modern Network

    Modern Network

    Let’s go back to after 2011. Imagine the following picture as a representation of a vehicle (You already did that…

  • Legacy Network Architecture

    Legacy Network Architecture

    Let’s go back before 2011. Imagine the following picture as a representation of a vehicle (yes, consider it a vehicle!…

  • Automotive Network Architecture from Legacy SOA to Modern SOA.

    Automotive Network Architecture from Legacy SOA to Modern SOA.

    1. Introduction One of the most exciting changes in the automotive industry is the shift in network design, moving from…

  • Embedded Linux Road Map Part #1

    Embedded Linux Road Map Part #1

    Hello ??, In this article and the following ones I would like to share a road map for Embedded Linux Software…

    7 条评论
  • Embedded Linux Diploma Batch 4 ????????????

    Embedded Linux Diploma Batch 4 ????????????

    Batch 4 is coming Next Friday (10/11/2023) god-willing ?????? Our diploma taken by +70 Engineers with overall 4.3/5 ??…

    8 条评论
  • Virtual Keyword in C++ (Part#1)

    Virtual Keyword in C++ (Part#1)

    4 条评论

社区洞察

其他会员也浏览了