c ++构造函数问题

Joh*_*ane 0 c++ constructor

我仍然对CTORs感到困惑:

问题1:
为什么第15行呼叫A:A(int)而不是A:A(double&)

问题2:
为什么18号线没有打电话A:A(B&)

#include <iostream>
using namespace std;

class B{};

class A{
public:
   A(int )    {cout<<"A::A(int)"<<endl;}
   A(double&){cout<<"A::A(double&)"<<endl;} // it will work if it is A(double), without the &
   A(B&){cout<<"A::A(B&)"<<endl;}
};

int main()
{
/*line 15*/   A obj((double)2.1);  // this will call A(int), why?
   B obj2;
   A obj3(obj2);
/*line 18*/   A obj4(B);          // this did not trigger any output why?
}
Run Code Online (Sandbox Code Playgroud)

ken*_*ytm 6

第15行: A(double&)只能取左值,即可以赋值的变量.(double)2.1是一个左值.A(const double&)如果您需要接受rvalues作为参考,请使用.

第18行: B是一种类型,而不是一种价值.A obj4(B);只声明一个名为obj4a B并且返回一个的函数A.