为啥编译器不报错呢
为什么编译器不报错呢?
void myFunction(const int);
void myFunction(int);
以上两行代码写在一起,编译器报错,可是如下代码:
void myFunction(const int&); //什么时候调用这个函数?
void myFunction(int&); // 什么时候调用这个函数?
编译器不报错,求解释注释里问题。
举例:
myFunction(12);
调用哪一个?
请问网上哪里有相关的资料,想看看。
------解决方案--------------------
楼主啊,不用我一定要说到第三次吧
买《C++ primer》认真啃,都有答案。
别浪费时间在网上东问西问的了。
------解决方案--------------------
1 const在函数名前 在参数里 在参数后面 这三种情况表达的含义先弄清楚
2 编译器编译函数时以参数的个数和参数类型来区别不同的函数 这点应该在函数重载时有很详细的阐述
------解决方案--------------------
刚才翻的标准这里可以用上了
ISO 2011标准 $13.1/3
Parameter declarations that differ only in the presence or absence of const and/or volatile are
equivalent. That is, the const and volatile type-specifiers for each parameter type are ignored when determining which function is being declared, defined, or called.
...
... In particular, for any type T, “pointer to T,” “pointer to const T,” and “pointer to volatile T” are considered distinct parameter types, as are “reference to T,” “reference to const T,” and “reference to volatile T.”
前一段说的是仅仅多一个const是没用的,后一段说的是const修饰的类型的引用例外
void myFunction(const int);
void myFunction(int);
以上两行代码写在一起,编译器报错,可是如下代码:
void myFunction(const int&); //什么时候调用这个函数?
void myFunction(int&); // 什么时候调用这个函数?
编译器不报错,求解释注释里问题。
举例:
myFunction(12);
调用哪一个?
请问网上哪里有相关的资料,想看看。
------解决方案--------------------
楼主啊,不用我一定要说到第三次吧
买《C++ primer》认真啃,都有答案。
别浪费时间在网上东问西问的了。
------解决方案--------------------
1 const在函数名前 在参数里 在参数后面 这三种情况表达的含义先弄清楚
2 编译器编译函数时以参数的个数和参数类型来区别不同的函数 这点应该在函数重载时有很详细的阐述
------解决方案--------------------
刚才翻的标准这里可以用上了
ISO 2011标准 $13.1/3
Parameter declarations that differ only in the presence or absence of const and/or volatile are
equivalent. That is, the const and volatile type-specifiers for each parameter type are ignored when determining which function is being declared, defined, or called.
...
... In particular, for any type T, “pointer to T,” “pointer to const T,” and “pointer to volatile T” are considered distinct parameter types, as are “reference to T,” “reference to const T,” and “reference to volatile T.”
前一段说的是仅仅多一个const是没用的,后一段说的是const修饰的类型的引用例外