C++重载函数匹配

huangapple go评论76阅读模式
英文:

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 &lt;iostream&gt;

using namespace std;

class Base {
public:
  void func();
  void func(int);
};

void Base::func() { cout &lt;&lt; &quot;Base::func()&quot; &lt;&lt; endl; }

void Base::func(int a) { cout &lt;&lt; &quot;Base::func(int)&quot; &lt;&lt; endl; }

class Derived : public Base {

public:
  void func(const string &amp;);
  void func(const string &amp;&amp;);
  void func(bool);
};

void Derived::func(const string &amp;s) {
  cout &lt;&lt; &quot;Derived::func(string &amp;)&quot; &lt;&lt; endl;
}

void Derived::func(const string &amp;&amp;s) {
  cout &lt;&lt; &quot;Derived::func(string &amp;&amp;)&quot; &lt;&lt; endl;
}

void Derived::func(bool b) { cout &lt;&lt; &quot;Derived::func(bool)&quot; &lt;&lt; endl; }

int main() {

  Derived d;
  d.func(&quot;biancheng&quot;);
  d.func(false);

  // d.func(1);
  // d.Base::func(1);

  cout &lt;&lt; endl &lt;&lt; &quot;completed ..&quot; &lt;&lt; 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 的指针转换。

英文:

&quot;biancheng&quot; 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&lt;char, /*char-traits-type and allocator*/&gt;. 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(&quot;biancheng&quot;);, 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 &amp;s) || d.func(const string &amp;&amp;s).

huangapple
  • 本文由 发表于 2023年7月27日 16:34:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76777913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定