简体   繁体   English

定义赋值运算符和复制构造函数

[英]Defining both Assignment operator and copy constructor

Suppose I have a class A. I have defined a copy constructor and an assignment operator overloading function. 假设我有一个类A。我定义了一个复制构造函数和一个赋值运算符重载函数。 When I do 当我做

Class A; A类; Class B=A; B类= A;

Then while defining Class B, is the copy constructor invoked or the assignment operator? 然后在定义B类时,是调用复制构造函数还是赋值运算符?

Thanks in advance. 提前致谢。

EDIT: Sorry, I mentioned wrong code. 编辑:对不起,我提到了错误的代码。 It should be: 它应该是:

A a; A a; A b=a; a b = a;

IIRC T t = value invokes the copy constructor, you can verify that by outputting a string in the constructors to determine which method is used. IIRC T t = value调用复制构造函数,可以通过在构造函数中输出字符串以确定所使用的方法来进行验证。 IIRC when the declaration and assignment are on the same line, it is not called assignment but initialization. IIRC当声明和赋值在同一行时,它不称为赋值,而是初始化。

On the other hand, what you've posted does not make sense, you cannot assign one type to another type, you can only assign to type instances. 另一方面,您发布的内容没有任何意义,您不能将一个类型分配给另一种类型,只能分配给类型实例。

EDIT: Even if you have a case of two different types (the context of your question is not clear on this one): 编辑:即使您有两种不同类型的情况(您的问题的上下文对此不清楚):

class A {};

class B {
public:
   B(const A& other)  { cout << "copy"; }
   B& operator=(const A& other) { cout << "assign"; }
};

int main() {
   A a;
   B b = a; // copy con
   B b1(a); // same as above
   b = a;   // assign op
}

Even then, when both the "copy constructor" and assignment operators take in another type, the copy constructor will still be invoked rather than the assignment operator. 即使这样,当“复制构造函数”和赋值运算符都采用另一种类型时,仍将调用复制构造函数而不是赋值运算符。

Assuming you actually mean something like: 假设您实际上的意思是:

A a;
A b = a;

The copy constructor is invoked. 复制构造函数被调用。 The Standard allows = this special meaning in this usage. 标准在此用法中允许=此特殊含义。

Create a simple class with both, and debug what function is executed by setting a breakpoint in both. 使用两者创建一个简单的类,并通过在两者中设置一个断点来调试要执行的功能。 Then youll see, and youll also learn a little bit of debugging. 然后您将看到,并且还将学习一些调试。

Let's try it out! 让我们尝试一下!

#include <iostream>

class A {
 public:
  A() {
    std::cout << "default constructor\n";
  }
  A(const A& other) {
    std::cout << "copy constructor\n";
  }
  A& operator=(const A& rhs) {
    std::cout << "assignment operator\n";
    return *this;
  }
};

int main(int argc, char** argv) {
  std::cout << "first declaration: ";
  A a;
  std::cout << "second declaration: ";
  A b(a);
  std::cout << "third declaration: ";
  A c = a;
  std::cout << "fourth declaration: ";
  A d;
  std::cout << "copying? ";
  d = a;
  return 0;
}

This prints: 打印:

first declaration: default constructor
second declaration: copy constructor
third declaration: copy constructor
fourth declaration: default constructor
copying? assignment operator

Working example here: http://codepad.org/DNCpqK2E 这里的工作示例: http : //codepad.org/DNCpqK2E

when you are defining a new object with assigning another object into it. 当您定义新对象并为其分配另一个对象时。 It invokes copy constructor. 它调用复制构造函数。 eg, Copy constructor call: 例如,复制构造函数调用:

Class A;
Class B=A;

Assignment operator call: 分配操作员呼叫:

Class A;
Class B;
B=A;

You can always test this, by writing a "print" statement in both the methods to find out which one is being called. 您始终可以通过在两种方法中编写“ print”语句来找出正在调用的方法来进行测试。

Hope it helped... 希望能有所帮助...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM