英文:
c++ overload functions match
问题
我对C++中的函数重载匹配感到困惑。
请看下面的代码:
#include <iostream>
using namespace std;
class Base {
public:
void func();
void func(int);
};
void Base::func() { cout << "Base::func()" << endl; }
void Base::func(int a) { cout << "Base::func(int)" << endl; }
class Derived : public Base {
public:
void func(const string &);
void func(const string &&);
void func(bool);
};
void Derived::func(const string &s) {
cout << "Derived::func(string &)" << endl;
}
void Derived::func(const string &&s) {
cout << "Derived::func(string &&)" << endl;
}
void Derived::func(bool b) { cout << "Derived::func(bool)" << endl; }
int main() {
Derived d;
d.func("biancheng");
d.func(false);
// d.func(1);
// d.Base::func(1);
cout << endl << "completed .." << endl;
return 0;
}
运行结果为:
Derived::func(bool)
Derived::func(bool)
completed ..
对于调用 d.func("biancheng");,打印的结果与 func(bool) 函数的定义相匹配。有人可以帮我理解是什么原因吗?
我原以为会匹配 func(const string &s) 或 func(const string &&s) 中的一个。
英文:
I felt confused for c++ overloaded functions mathing.
see code below:
#include <iostream>
using namespace std;
class Base {
public:
void func();
void func(int);
};
void Base::func() { cout << "Base::func()" << endl; }
void Base::func(int a) { cout << "Base::func(int)" << endl; }
class Derived : public Base {
public:
void func(const string &);
void func(const string &&);
void func(bool);
};
void Derived::func(const string &s) {
cout << "Derived::func(string &)" << endl;
}
void Derived::func(const string &&s) {
cout << "Derived::func(string &&)" << endl;
}
void Derived::func(bool b) { cout << "Derived::func(bool)" << endl; }
int main() {
Derived d;
d.func("biancheng");
d.func(false);
// d.func(1);
// d.Base::func(1);
cout << endl << "completed .." << endl;
return 0;
}
and the results:
Derived::func(bool)
Derived::func(bool)
completed ..
For calling <code>d.func("biancheng");</code>, the result printed matched the func(bool) function definition. Can somebody help me understand what is the cause?
I thought it would be one of the func(const string &s) or func(const string &&s).
答案1
得分: 3
"biancheng" 是类型为 char const[10] 的变量。它会转化为 char const*。从这里开始,有两种可能的转换方式。一种是隐式(内置)转换为 bool,另一种是隐式转换为类类型 std::basic_string<char, /*char-traits-type and allocator*/>。类类型的转换总是在内置转换之后考虑的,所以编译器选择了指向 bool 的指针转换。
英文:
"biancheng" is of type char const[10]. It decays to char const*. From here there are two possible conversions. There is the implicit (built in) conversion to bool and the implicit conversion to the class type std::basic_string<char, /*char-traits-type and allocator*/>. Conversions to class types are always considered after built in conversions, so the pointer to bool conversion is chosen here by the compiler.
答案2
得分: -1
在你的代码中,当你调用函数 d.func("biancheng"); 时,它与 d.func(bool b) 匹配,因为字符串参数 "biancheng" 可以隐式转换为 TRUE [布尔参数]。因此,调用了 d.func(bool b) 而不是 d.func(const string &s) 或 d.func(const string &&s)。
英文:
In your code, when you called function d.func("biancheng");, it matched with d.func(bool b) because the string parameter "biancheng" can be implicitly converted to TRUE [boolean parameter]. Therefore, d.func(bool b) was called instead of d.func(const string &s) || d.func(const string &&s).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论