简体   繁体   English

C ++:将一个对象复制到构造函数中的另一个对象

[英]C++: Copy one object to another within a constructor

In my code, I am trying to make a string class (it's an assignment) and have a class with the following data: 在我的代码中,我试图创建一个字符串类(这是一个赋值),并使用一个包含以下数据的类:

private:
  int   strLen;
  int   dataLen;
  char* data;

and the constructors: 和构造函数:

myStr();
myStr(const myStr&);
myStr(const char*);

The third constructor takes a string array, allocates space to data and copies it in, sets the strLen to the amount of characters in the string, and dataLen to the size of the array. 第三个构造函数使用字符串数组,为数据分配空间并将其复制到其中,将strLen设置为字符串中的字符数,并将dataLen设置为数组的大小。 The second constructor is similar, however it takes an entire object and is supposed to copy it into the new object that the constructor is creating. 第二个构造函数是类似的,但是它需要一个完整的对象,并且应该将其复制到该构造函数正在创建的新对象中。 An example could look like this: 一个示例可能如下所示:

myStr fred("Fred");
myStr quote(fred);

My question is, is there a way to copy object fred to object quote in the constructor without copying each individual piece of data (ie strLen, dataLen and data)? 我的问题是,有没有一种方法可以将对象fred复制到构造函数中的对象引用中,而不复制每个单独的数据(即strLen,dataLen和data)? For this assignment, there are only 3 pieces of data, but say there were 50, or 100, it would be long and tedious. 对于此分配,只有3条数据,但是说有50条或100条数据将是冗长而乏味的。 I tried searching for an answer and came across copy-constructors, however I was left uncertain how they work, or if they are just manually copying each piece of data anyway. 我试图寻找答案并遇到了复制构造函数,但是我不确定它们如何工作,或者它们是否只是手动复制每个数据。 I am new to the syntax of classes so I apologize if the answer was trivial and I was just looking for answers in all the wrong places. 我是类语法的新手,所以如果答案是微不足道的,而我只是在所有错误的地方寻找答案,我深表歉意。 Thank you. 谢谢。

is there a way to copy object fred to object quote in the constructor without copying each individual piece of data (ie strLen, dataLen and data)? 有没有一种方法可以将对象fred复制到构造函数中的对象引用,而无需复制每个单独的数据(即strLen,dataLen和data)?

Nope. 不。 The job of a copy constructor (and copy assignment operator) is to copy each individual field as needed. 复制构造函数(和复制分配运算符)的工作是根据需要复制每个单独的字段。 And in your case, since your data is dynamically allocated, that also means allocating a new memory block to copy the character data into. 在您的情况下,由于数据是动态分配的,因此这还意味着分配新的内存块以将字符数据复制到其中。

For this assignment, there are only 3 pieces of data, but say there were 50, or 100, it would be long and tedious. 对于此分配,只有3条数据,但是说有50条或100条数据将是冗长而乏味的。

Yes, it would be, which is why: 是的,这就是原因,这是为什么:

  1. classes should be as short and precise as possible. 课程应尽可能简短和准确。 Larger, more complex, classes can then be designed using smaller classes to manage data efficiently. 然后可以使用较小的类来设计更大,更复杂的类,以有效地管理数据。

  2. you should be using data types that the compiler can copy for you. 您应该使用编译器可以为您复制的数据类型。 If you do not provide a copy constructor (or copy assignment operator), the compiler generates a default one for you (unless you tell the compiler not to do so - C++11 and later only) that does a member-by-member copy. 如果不提供副本构造函数(或副本赋值运算符),则编译器会为您生成一个默认值(除非您告诉编译器不要这样做-仅限C ++ 11和更高版本),它会逐个成员地进行复制。 That default implementation works fine for trivial data types, and user-defined types that implement proper copy semantics (copy constructor and copy assignment operator). 该默认实现适用于琐碎的数据类型以及实现适当的复制语义(复制构造函数和复制赋值运算符)的用户定义类型。

In your example, if you replaced your three fields with a single std::string field, your class would not need to implement custom copy semantics anymore, the compiler-generated copy constructor and copy assignment operator would be sufficient. 在您的示例中,如果用单个std::string字段替换了三个字段,则您的类将不再需要实现自定义副本语义,编译器生成的副本构造函数和副本赋值运算符就足够了。

If you need to worry about implementing manual memory management when copying data, read up about the Rule of Three and why proper copy semantics are important. 如果您在复制数据时需要担心执行手动内存管理,请阅读规则以及正确的复制语义为何重要的原因。

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

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