I am still confused with the CTORs:
Question 1:
why line 15 call A:A(int) instead of A:A(double&)?
Question 2:
why line 18 did not call 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?
}
Line 15:
A(double&)can only take lvalues, i.e. variables that can be assigned to.(double)2.1is an rvalue. UseA(const double&)if you need to accept rvalues as reference too.Line 18:
Bis a type, not a value.A obj4(B);only declares a function with nameobj4taking aBand returning anA.