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 d.func("biancheng");
, 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).