声明变量时 = 与复制构造函数 c++ 相同吗?

发布于 2024-12-28 00:10:25 字数 390 浏览 0 评论 0原文

我安排了上课时间 并在 main 中声明:

time a;
time b=a;

相同吗

time b(a);

这与: ??? ? 我还制作了函数:

time f(time t)
{
  return t;
}

并在声明 a 后在 main 中使用它:

time b=f(a);

当调用复制构造函数时,我打印了消息,结果只是对复制构造函数的两次调用,一个将 a 复制到 t,另一个将 t 复制到返回,这是我的问题没有调用复制构造函数来将从函数返回的值复制到 b ?

提前致谢 !

I made class time ,
and declared in main :

time a;
time b=a;

is this the same as :

time b(a);

???
I also made function :

time f(time t)
{
  return t;
}

and used it in main after declaring a :

time b=f(a);

I printed messages when copy constructor called , the Result was only 2 calls to copy constructor ,one copying a to t, the other is copying t to return , here is my question there was no call to copy constructor to copy the value returned from function to b ?

thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

花桑 2025-01-04 00:10:25

您标题中的问题和帖子底部的问题是不同的。对于标题中的一个:

声明变量时 = 与 C++ 复制构造函数相同吗?

不完全是。首先将右侧的表达式转换为左侧的类型,然后调用复制构造函数。这需要隐式转换。如果不可能,初始化将无法编译。如果右侧的表达式已经是正确的类型,则不需要第一步。

对于您的其他问题,这是返回值优化

The question in your header and the question at the bottom of your post are different. For the one in your header:

does = when declaring a variable the same as copy constructor c++?

Not exactly. First the expression on the right side is converted to the type on the left side, and then the copy constructor is called. This requires an implicit conversion. If one is not possible, the initialization will not compile. If the expression on the right side is already the right type, then the first step is not necessary.

For your other question, this is return value optimization.

坚持沉默 2025-01-04 00:10:25

是的,time b = a 相当于 time b( a ),因为您在声明对象的同时为其赋值。

对于问题的第二部分,编译器通常会根据优化级别删除不必要的对象副本。

Yes, time b = a is equivalent to time b( a ) because you are declaring the object and assigning its value at the same time.

For the second part of your question, the compiler will in general remove unnecessary object copies where it can, depending on the optimization level.

眼波传意 2025-01-04 00:10:25

当从另一个对象创建一个对象时,复制构造函数会被调用,如

time b = a;
或时间b(a);

但是当对象已经创建并且您想要将项目复制到其中时,则操作符=
被称为

区别就像当你使用动态分配的内存时
如果您使用复制构造函数,则意味着您尚未创建,因此您不需要对内存管理进行任何清理,

但是当您将一个对象分配给另一个对象时,它首先必须处理其自己的动态分配的元素,就像空闲内存一样,然后将它们复制到自己的内存中

when an object is being created from another object, the copy constructor is called like

time b = a;
or time b(a);

but when the object is already created and you want to copy items to it, then the operator=
is called

the difference is like when you are using dynamically allocated memory
if you use copy constructor, that means that you are not already created, therefore you don't need any cleanup of your memory management

but when you assign an object to another, first it has to deal with the dynamically allocated elements of its own, like free memory, then copy them to its own

空心空情空意 2025-01-04 00:10:25

不,b=a 使用赋值运算符 (operator=) 创建一个新的空白时间对象,然后调用该运算符将值移入其中(或者您告诉 operator= 执行的其他操作)。

通常,您会发现编译器生成的版本会调用复制构造函数,但并非必须如此。

作为参考,请查找 explicit 关键字。由于复制对象可以通过两种方式完成(复制构造函数和赋值运算符),这告诉编译器仅在显式调用时使用复制构造函数,而不是隐式调用。

使用所有不同的构造函数(包括新的 c++0x move one)和赋值运算符创建您的类,并查看通过调试器运行它时会发生什么。

no, b=a uses the assignment operator (operator=) to create a new, blank time object and then call the operator to move the values into it (or whatever else you tell operator= to do).

Often you'll find the compiler-generated versions of this will call the copy constructor, but it doesn't have to.

For reference, look up the explicit keyword. As copying an object can be done in 2 ways (copy ctor, and assignment operator), this tells the compiler to only use the copy ctor when explicitly invoked, not implicitly.

Create your class with all the different constructors (including the new c++0x move one) and the assignment operator and look what happens when you run it through the debugger.

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