类的成员复制 [英] member copying of class
问题描述
在学习复制成员的概念时,本书给出了以下语句。
此外,默认分配不能如果非静态成员是引用,常量或没有副本分配的用户定义类型,则生成。
了解这个声明真正想要提供什么?或者这种语句涉及哪种情况?感谢。
此语句与编译器自动为您编写的类生成默认赋值运算符函数定义类型)。默认分配通过将所有成员复制到新实例来工作。此语句包含三种情况,其中默认分配不会能够生成:
1)当成员是引用一个变量的实例,如一个指针)
class Foop {
int&参考;
};
2)当成员变量为常数时
class Foople {
const int someConst;
};
3)当一些其他类没有复制构造函数并且你有一个成员变量类型,显然不能使用默认方法(使用复制构造函数)复制
class Uncopyable {
private :
Uncopyable(Uncopyable const& other);
};
class Fleep {
不可复制不可复制;
};
在这些情况下,您需要编写自己的赋值运算符/ p>
While learning the concept of "copying members", the book gives the following statement.
In addition, a default assignment cannot be generated if a nonstatic member is a reference, a const,or a user-defined type without a copy assignment.
I do not quite understand what does this statement really want to deliver? Or which kind of scenario does this statement refer to? Thanks.
This statement has to do with the compiler automatically generating the default assignment operator function for a class you write (i.e. user-defined type). The default assignment works by copying all the members over to a new instance. This statement covers three cases where a default assignment would not be able to be generated:
1) When a member is a reference (i.e. refers to an instance of a variable, like a pointer)
class Foop {
int& reference;
};
2) When a member variable is constant
class Foople {
const int someConst;
};
3) When some other class does not have a copy-constructor and you have a member variable of that type, obviously it cannot be copied using the default method (which uses copy-constructors)
class Uncopyable {
private:
Uncopyable(Uncopyable const& other);
};
class Fleep {
Uncopyable uncopyable;
};
In these cases, you would need to write your own assignment operator (or possibly do without).
这篇关于类的成员复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!