了解函数原型和函数重载
谁能给我一个 C++ 中具有 4 个函数原型的函数重载的示例?我还是不太明白..
抱歉新手问题,感谢您的关注。Adam
Ramadhan
can anyone give me an example of function overloading in c++ with 4 function prototypes ? i still don't get them quite good ..
sorry newbie question, thanks for looking in.
Adam Ramadhan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
以下是 C++ 函数声明,通常位于头文件(.h 或 .hpp)中。这些特定的声明没有代码。代码位于下面进一步显示的定义中。
上述四个函数具有相同的名称,但 C++ 编译器将调用参数签名与调用代码中的参数签名匹配的函数。声明的目的是告诉编译器函数的返回类型和参数签名是什么。如果多个函数具有相同的名称但其参数签名不同,则称为重载。这是 C++ 中不存在的功能。请注意,返回类型不能用于区分重载函数。
以下是重载函数的定义(实现),通常位于模块(.cpp 或 .cc 或 .cxx)文件中。这是可执行代码位于包围功能块的大括号 { } 之间的位置:
使用示例:
将调用上面列出的第二个重载函数的代码,该函数采用三个
int
参数。The following are C++ function declarations and would typically be in the header (.h or .hpp) file. These particular declarations have no code. The code is in the definition shown further below.
The above four functions have the same name, but the C++ compiler will call the one whose parameter signature matches the one in the calling code. The purpose of a declaration is to tell the compiler what the return type and parameter signature of a function are. If more than one function has the same name but differs in its parameter signature, it is referred to as overloaded. This is a C++ feature not present in C. Note that the return type cannot be used to differentiate overloaded functions.
The following are the definitions (implementations) of the overloaded functions and would typically be in the module (.cpp or .cc or .cxx) file. This is where the executable code resides between the braces { } that surround the function block:
Usage example:
will invoke the code for the second overloaded function listed above that takes three
int
parameters.