C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 重载函数中数据类型的优先级如何?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有3个函数重载.重载函数中数据类型的优先级如何?

#include <iostream>

using namespace std;

void MyFunc (int i) {
    cout << "int" << endl;
}

void MyFunc (double i) {
    cout << "double" << endl;
}

void MyFunc (float i) {
    cout << "float" << endl;
}

int main () {
    MyFunc(1);
    float x = 1.0;
    MyFunc(X);
    MyFunc(1.0);
    MyFunc(15.0);
    return 0;
}

输出

int
float
double
double

程序如何决定调用float还是double?

解决方法

文字有明确定义的类型.特别是,除非加后缀,否则 floating-point literals具有double类型.后缀f或F使其成为float类型的文字,而后缀l或L使其成为long double类型的文字.

这解释了观察到的重载分辨率:

MyFunc(X);//calls MyFunc(float) since x is a float
MyFunc(1.0);//calls MyFunc(doublE) since 1.0 is a double
MyFunc(15.0);//calls MyFunc(doublE) since 15.0 is a double

类似的推理也适用于integer literals – 1是int类型的文字.

大佬总结

以上是大佬教程为你收集整理的c – 重载函数中数据类型的优先级如何?全部内容,希望文章能够帮你解决c – 重载函数中数据类型的优先级如何?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。