c++构造函数问题
我仍然对 CTOR 感到困惑:
问题 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?
}
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?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第 15 行
您的引用构造函数仅接受对非 const 对象的引用。
也就是说,您写的是:
而不是:
问题在于,在
A obj((double)2.1)
中,您使用临时对象调用了构造函数(在此在这种情况下,双精度字面量2.1
(强制转换为double
是没有意义的),并且临时对象可能不会绑定到不是对const
引用的引用 。所以 无法调用
A(double&)
构造函数;唯一可能匹配的是A(int)
,并且对您的数据执行所需的转换 (2.1
→2
)来实现这一点(您的编译器通常会警告您 发生这种情况。)因此,使用以下形式 接受对
const
的引用的构造函数第 18 行
对于
A obj4(B)
,B
是一种类型,因此您要声明一个函数。名为obj4
,它接受(未命名)B
并返回A
。Line 15
Your reference constructors accept only references to non-
const
objects.That is, you wrote:
instead of:
The problem with this is that, in
A obj((double)2.1)
, you invoked the constructors with temporary objects (in this case, the double literal2.1
(that cast todouble
is pointless), and temporary objects may not bind to references that are not references toconst
.So the
A(double&)
constructor cannot be called; the only one left that might match isA(int)
, and the required conversion is performed on your data (2.1
→2
) to make this happen. (Your compiler will normally warn you that this occurring.)So, use the form of constructor that accepts reference to
const
.Line 18
As for your
A obj4(B)
,B
is a type so you're declaring a function namedobj4
that takes an (unnamed)B
and returns anA
.因为
&
意味着它是一个引用类型,并且在第 15 行您传递的是一个值,而不是一个引用。Because
&
means it is a reference type and on line 15 you pass a value, not a reference.第 15 行,double 是右值(临时的),无法转换为非常量引用。对于第 18 行,最可能的解释是编译器只是优化了构造函数调用。
Line 15, the double is an rvalue (a temporary) and cannot convert to a non-const reference. For Line 18, the most likely explanation is that your compiler simply optimized out the constructor call.
第15行:
A(double&)
只能接受左值,即可以赋值的变量。(double)2.1
是一个右值。如果您也需要接受右值作为引用,请使用A(const double&)
。第 18 行:
B
是类型,而不是值。A obj4(B);
仅声明一个名为obj4
的函数,接受B
并返回A
。Line 15:
A(double&)
can only take lvalues, i.e. variables that can be assigned to.(double)2.1
is an rvalue. UseA(const double&)
if you need to accept rvalues as reference too.Line 18:
B
is a type, not a value.A obj4(B);
only declares a function with nameobj4
taking aB
and returning anA
.