Chinaunix首页 | 论坛 | 博客
  • 博客访问: 359870
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-22 09:53:43


Function overloading is the practice of declaring the same function with different signatures. The same function name will be used with different number of parameters and parameters of different type. But overloading of functions with different return types are not allowed.

For example in this C++ Tutorial let us assume an AddAndDisplay function with different types of parameters.

//C++ Tutorial - Sample code for function overloading
    void AddAndDisplay(int x, int y)
    {
       cout<<" C++ Tutorial - Integer result: "<<(x+y);
    }

    void AddAndDisplay(double x, double y)
    {
       cout<< " C++ Tutorial - Double result: "<<(x+y);
    }

    void AddAndDisplay(float x, float y)
    {
       cout<< " C++ Tutorial - float result: "<<(x+y);

    }

 Some times when these overloaded functions are called, they might cause ambiguity errors. This is because the compiler may not be able to decide what signature function should be called.

If the data is type cast properly, then these errors will be resolved easily. Typically, function overloading is used wherever a different type of data is to be dealt with. For example this can be used for a function which converts farenheit to celsius and vice versa. One of the functions can deal with the integer data, other can deal float for precision etc.,


阅读(1262) | 评论(1) | 转发(0) |
0

上一篇:typedef & #define

下一篇:This 指针

给主人留下些什么吧!~~

onezeroone2011-04-24 16:00:44

with different number of parameters and parameters of different type,
different return types are not allowed