C++复制构造函数调用

发布于 2024-10-23 20:27:26 字数 317 浏览 1 评论 0原文

快问。如果我有一个数组并且已经正确重载了赋值运算符,那么当我执行以下操作时:

A = B 

当 A 和 B 都是数组类型的对象时,我是调用复制构造函数,还是仅调用重载的赋值运算符(=)?

时,会调用复制构造函数

  1. “传递值”
  2. 返回类类型的值
  3. 我知道当一个对象被括号中给出的相同类型的另一个对象声明和初始化时,当

。上面的3让我很困惑,认为A = B也在调用复制构造函数。

难道只是调用重载的赋值运算符吗?

谢谢!

Quick question. If I have an array and have properly overloaded the assignment operator, then when I do something like this:

A = B 

When A and B are both objects of type array, am I calling the copy constructor, or just the overloaded assignment operator(=)?

I know that a copy constructor is called when

  1. Pass By value
  2. return a value of class type
  3. when an object is being declared and initialized by another object of the same type given in parenthesis.

3 above makes me confused and thinking that A = B is also calling the copy constructor.

Is it just calling the overloaded assignment operator?

Thanks!

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

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

发布评论

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

评论(5

遗弃M 2024-10-30 20:27:27

上面的3让我很困惑,认为A = B也在调用复制构造函数。

除非您的复制构造函数被声明为显式,否则以下内容确实会调用复制构造函数:

MyType A = B; // will call copy ctor

我相信标准保证了这一点(我手头没有参考资料),所以您知道您不会调用默认值ctor 后跟赋值运算符。

3 above makes me confused and thinking that A = B is also calling the copy constructor.

Unless your copy constructor is declared explicit, the following will indeed call the copy constructor:

MyType A = B; // will call copy ctor

I believe the standard guarantees this (I don't have my reference handy) so you know you will not be calling the default ctor followed by the assignment operator.

冷…雨湿花 2024-10-30 20:27:26

以上都不是:您不能分配数组。


如果您有自己的数组类,并且它具有如下所示的内容:

struct arr
{
    arr& operator=(const arr& other)
    {
        // ...
    }
};

arr a, b;

那么这些是等效的:

a = b;
a.operator=(b);

就像调用函数一样。

None of the above: you cannot assign arrays.


If you have your own array class, and it has something like this:

struct arr
{
    arr& operator=(const arr& other)
    {
        // ...
    }
};

arr a, b;

Then these are equivalent:

a = b;
a.operator=(b);

It's just like calling a function.

十二 2024-10-30 20:27:26

既然您已经说过该数组是您自己的带有重载赋值运算符的类,那么您已经回答了您自己的问题。

复制构造函数实际上仅在您从另一个对象构造对象时才会被调用:

Obj a;
目标 b(a);

如果你这样做的话,它最终可能会被某种编译器魔术调用:

Obj a;
对象 b = a;

但我从来没有费心去真正查找。

如果你只是执行 a = b 你并没有构造 a,因此你不会调用复制构造函数。

有道理吗?

Since you've said the array is your own class with an overloaded assignment operator then you've already answered your own question.

The copy constructor is literally only called when you are constructing the object from another:

Obj a;
Obj b(a);

It might wind up being called by some sort of compiler magic if you do:

Obj a;
Obj b = a;

But I never bothered to actually look that up.

If you just do a = b you are not constructing a, therefore you'd not call the copy constructor.

Make sense?

绾颜 2024-10-30 20:27:26

如果执行A=B;,则调用重载赋值运算符

class userDefinedArray
{
    int size ;
    int* ptr;

    public:
    userDefinedArray( int size ) :   size(size)
                                   , ptr( new int[size] )
    {}
    // The Big Three = 1. Copy Constructor 2. Assignment Operator 3. Destructor
};

如果上面是类定义,则应调用赋值运算符。

Overloaded Assignment Operator is called if performed A=B;

class userDefinedArray
{
    int size ;
    int* ptr;

    public:
    userDefinedArray( int size ) :   size(size)
                                   , ptr( new int[size] )
    {}
    // The Big Three = 1. Copy Constructor 2. Assignment Operator 3. Destructor
};

If the above is the class definition, then assignment operator should be called.

路还长,别太狂 2024-10-30 20:27:26

正如您所说,当

  1. 一个对象被括号中给出的相同类型的另一个对象声明和初始化时,当“传递值”
  2. 返回类类型的值
  3. 时,将调用复制构造函数或作为赋值。

考虑以下代码的输出:

#include <iostream>

using std::cout;

class Test
{
public:
    Test()
    {
    }
    Test(const Test& T)
    {
        cout << "Calling Copy Constructor";
    }
};

void PassingValue(Test T)
{
    cout << " while Passing parameter by value\n\n";
}

void PassingReference(Test &T)
{
    cout << "NOT CALLING copy constructor while Passsing Parameter by reference\n\n";
}

int main()
{
    Test One;
    // case 1 :
    cout << "case 1 : \n";
    Test Two(One);
    cout << " while creating Two as a copy of One\n\n";
    // case 2 :
    cout << "case 2 : \n";
    PassingValue(One);
    // case 3 :
    cout << "case 3 : \n";
    Test Three = Two;
    cout << " while assigning initial value\n\n";
    // case 4 :
    cout << "case 4 : \n";
    PassingReference(Two);
    // case 5
    cout << "case 5 : \n";
    Test Four;
    Four = Three;
    cout << "NOT CALLING copy constructor while assigning normally\n\n";
    return 0;
}

Copy constructor is called -as you said- when

  1. Pass By value
  2. return a value of class type
  3. when an object is being declared and initialized by another object of the same type given in parenthesis or as an assignment.

Consider the output of the following code :

#include <iostream>

using std::cout;

class Test
{
public:
    Test()
    {
    }
    Test(const Test& T)
    {
        cout << "Calling Copy Constructor";
    }
};

void PassingValue(Test T)
{
    cout << " while Passing parameter by value\n\n";
}

void PassingReference(Test &T)
{
    cout << "NOT CALLING copy constructor while Passsing Parameter by reference\n\n";
}

int main()
{
    Test One;
    // case 1 :
    cout << "case 1 : \n";
    Test Two(One);
    cout << " while creating Two as a copy of One\n\n";
    // case 2 :
    cout << "case 2 : \n";
    PassingValue(One);
    // case 3 :
    cout << "case 3 : \n";
    Test Three = Two;
    cout << " while assigning initial value\n\n";
    // case 4 :
    cout << "case 4 : \n";
    PassingReference(Two);
    // case 5
    cout << "case 5 : \n";
    Test Four;
    Four = Three;
    cout << "NOT CALLING copy constructor while assigning normally\n\n";
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文