【发布时间】:2017-01-02 13:34:20
【问题描述】:
我正在重载一个函数,它只是打印它作为参数接收的值。这是代码
#include<iostream>
using namespace std;
void show(int c)
{
cout<<"Int C : "<<c<<endl;
}
void show(char c)
{
cout<<"char C : "<<c<<endl;
}
void show(float c)
{
cout<<"Float C : "<<c<<endl;
}
void show(string c)
{
cout<<"String C : "<<c<<endl;
}
void show(const char* c)
{
cout<<"char *C : "<<c<<endl;
}
main()
{
string s("Vector");
show(25);
show('Z');
show("C++");
show("Hello");
show(4.9f);
show(s);
}
这里 show(65) 调用整数函数。
有没有可能我可以简单地调用 show(65) 来打印 ASCII 等效值 'A',即调用 show(char) 而不调用 show(int)
【问题讨论】:
标签: c++ function overloading