c++构造函数问题

发布于 2024-11-25 15:15:15 字数 730 浏览 0 评论 0原文

我仍然对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柠檬色的秋千 2024-12-02 15:15:16

第 15 行

您的引用构造函数仅接受对非 const 对象的引用。

也就是说,您写的是:

A(int)     { cout<<"A::A(int)"<<endl; }
A(double&) { cout<<"A::A(double&)"<<endl; }
A(B&)      { cout<<"A::A(B&)"<<endl; }

而不是:

A(int)           { cout<<"A::A(int)"<<endl; }
A(double const&) { cout<<"A::A(double const&)"<<endl; }
A(B const&)      { cout<<"A::A(B const&)"<<endl; }

问题在于,在 A obj((double)2.1) 中,您使用临时对象调用了构造函数(在此在这种情况下,双精度字面量 2.1 (强制转换为 double 是没有意义的),并且临时对象可能不会绑定到不是对 const 引用的引用 。

所以 无法调用 A(double&) 构造函数;唯一可能匹配的是 A(int),并且对您的数据执行所需的转换 (2.12)来实现这一点(您的编译器通常会警告您 发生这种情况。)

因此,使用以下形式 接受对 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:

A(int)     { cout<<"A::A(int)"<<endl; }
A(double&) { cout<<"A::A(double&)"<<endl; }
A(B&)      { cout<<"A::A(B&)"<<endl; }

instead of:

A(int)           { cout<<"A::A(int)"<<endl; }
A(double const&) { cout<<"A::A(double const&)"<<endl; }
A(B const&)      { cout<<"A::A(B const&)"<<endl; }

The problem with this is that, in A obj((double)2.1), you invoked the constructors with temporary objects (in this case, the double literal 2.1 (that cast to double is pointless), and temporary objects may not bind to references that are not references to const.

So the A(double&) constructor cannot be called; the only one left that might match is A(int), and the required conversion is performed on your data (2.12) 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 named obj4 that takes an (unnamed) B and returns an A.

匿名的好友 2024-12-02 15:15:16

因为 & 意味着它是一个引用类型,并且在第 15 行您传递的是一个值,而不是一个引用。

Because & means it is a reference type and on line 15 you pass a value, not a reference.

蛮可爱 2024-12-02 15:15:16

第 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.

怂人 2024-12-02 15:15:15

第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. Use A(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 name obj4 taking a B and returning an A.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文