简体   繁体   English

为什么复制构造函数被调用,即使我实际上是在C ++中复制到已经创建的对象?

[英]why copy constructor is getting called even though I am actually copying to already created object in C++?

I wrote below code and I didn't understand why copy constructor is getting called. 我写下面的代码,我不明白为什么复制构造函数被调用。

#include <iostream>

using namespace std;

class abc
{
    public:
        abc()
        {
            cout << "in Construcutor" << (this) << endl;
        };

        ~abc()
        {
            cout << "in Destrucutor" << (this) << endl;
        };

        abc(const abc &obj)
        {
            cout << "in copy constructor" << (this) << endl;
            cout << "in copy constructor src " << &obj << endl;
        }

        abc& operator=(const abc &obj)
        {
            cout << "in operator =" << (this) << endl;
            cout << "in operator = src " << &obj << endl;
        }
};

abc myfunc()
{
    static abc tmp;

    return tmp;
}

int main()
{
    abc obj1;

    obj1 = myfunc();

    cout << "OK. I got here" << endl;
}

when I ran this program, I am getting the following output 当我运行这个程序时,我得到以下输出

in Construcutor0xbff0e6fe
in Construcutor0x804a100
in copy constructor0xbff0e6ff
in copy constructor src 0x804a100
in operator =0xbff0e6fe
in operator = src 0xbff0e6ff
in Destrucutor0xbff0e6ff
OK. I got here
in Destrucutor0xbff0e6fe
in Destrucutor0x804a100

I didn't understand why copy constructor is getting called when I was actually assigning the object. 我不明白为什么在我实际分配对象时调用复制构造函数。

If I declare abc tmp , instead of static abc tmp in myfunc() , then copy constructor is not getting called. 如果我在myfunc()声明abc tmp而不是static abc tmp ,则不会调用复制构造函数。 Can any one please help me to understand what is going on here. 任何人都可以帮助我了解这里发生了什么。

Because you return an object by value from myfunc , which means it's getting copied. 因为你从myfunc 按值返回一个对象,这意味着它被复制了。

The reason the copy-constructor is not getting called if you don't make the object static in myfunc is because of copy elision and return value optimization (aka RVO). 拷贝构造函数,如果你不使对象没有被调用的原因staticmyfunc是因为复制省略返回值优化 (又名RVO)。 Note that even though your copy-constructor may not be called, it still has to exist. 请注意,即使您的复制构造函数可能未被调用,它仍然必须存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使由于C ++中的RVO而未调用复制构造函数,也如何复制成员变量的值 - How values of member variables are getting copied even though copy constructor is not getting called due to RVO in C++ 当我只返回对象c ++的引用时,为什么调用复制构造函数 - Why the copy constructor called, when I return just a reference of the object c++ C ++为什么复制构造函数被调用? - C++ Why was the copy constructor called? 构造函数调用已创建的对象 - Constructor called on an already created object 即使我通过移动语义传递给 lambda 捕获,但它仍然尝试使用复制构造函数进行构造 | C++ - even though i passed to lambda capture through move semantic, but it still try construct with copy constructor | C++ 什么时候在c ++中调用了副本构造函数,当我将一个对象分配给另一个对象时不调用它吗? - When is a copy constructor called in c++, is not it called when I assign a object to another object? C ++模板构造函数,为什么要调用拷贝构造函数? - C++ Template constructor, why is copy constructor being called? 当对象包含在c ++中的另一个对象中时,为什么复制构造函数被调用两次? - why the copy constructor is called twice when the object is contained in another object in c++? 为什么 C++ 链接器在构建过程中需要库文件,即使我是动态链接的? - Why does the C++ linker require the library files during a build, even though I am dynamically linking? 在遍历字符串时,即使我已经超出了长度,为什么还没有超出范围错误? - While iterating through a string, why am I NOT getting out of range error, even though I'm already beyond its length?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM