C++ 中的可复制数据类
受到这篇文章的启发,我有一个包含嵌套可复制数据类的类。此可复制类对其成员类没有动态内存分配、保护或私有内存。
Class Domain
{
public:
// copyable class
struct CopyableDataClass
{
int i;
}
void method1(const CopyableDataClass& data){...}
void method2(const CopyableDataClass& data){...}
}
另外,通过阅读此维基百科页面,我无法对“CopyableDataClass”本身使用动态内存分配,为了避免浅复制,比如
Domain::CopyableDataClass* p1=new CopyableDataClass();
Domain::CopyableDataClass* p2;
p2=p1;
但是“Domain”类呢,它也很特殊吗,例如,我可以按如下方式进行吗?
Class User
{
public:
Domain* getter();
void setter(const Domain* data);
private:
Domain* m_data; //pointer ok? private ok?
}
或者我必须像可复制类一样将其放入公共,
Class User
{
public:
Domain m_data;
}
除了一般类设计规则(封装等)之外,对于这个“域”类的使用是否有任何约束(保护/私有/动态内存分配)。我猜这些约束仅适用于“Copyable”类。我说得对吗?
感谢您的任何评论。
Inspired by this article, I have a class which contains a nested copyable data class. This copyable class has no dynamic memory allocation, protect or private for its member class.
Class Domain
{
public:
// copyable class
struct CopyableDataClass
{
int i;
}
void method1(const CopyableDataClass& data){...}
void method2(const CopyableDataClass& data){...}
}
Also, by reading this Wikipedia page, I could not use dynamic memory allocation for "CopyableDataClass" itself, to avoid the shallow copy, like
Domain::CopyableDataClass* p1=new CopyableDataClass();
Domain::CopyableDataClass* p2;
p2=p1;
But what about the "Domain" Class, is it also special, e.g., can I do it as follows?
Class User
{
public:
Domain* getter();
void setter(const Domain* data);
private:
Domain* m_data; //pointer ok? private ok?
}
Or I have to put it into public like the copyable class,
Class User
{
public:
Domain m_data;
}
Besides the general class design rule (encapsulation, etc.), is there any constraint (protect/private/dynamic memory allocation) to the usage of this "Domain" class. I guess the constraints only apply to the "Copyable" class. Am I right?
Thanks for any comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的CopyableDataClass就像一个值,类似于int。动态分配它并没有什么好处,就像您通常不会对单个 int 进行分配一样。
如果您的
Domain
类仅包含“可复制数据”,那么它将与其内容一样可复制。Your
CopyableDataClass
is like a value, similar to anint
. There is nothing much to gain from allocating it dynamically, just like you generally don't do that with singleint
s.If your
Domain
class contains just "copyable data" it will be just as copyable as its content.您的用户类别应该如下所示:
Your user class should look like that:
对于复制语义,我通常遵循以下规则:
1)如果我的类是 POD,我可以使用编译器生成的复制构造函数和赋值运算符。
2) 如果我想防止复制我的对象,我将复制构造函数和赋值运算符声明为私有,并且我不实现它们
3) 如果我的类不是 POD,我提供复制构造函数和赋值运算符的实现,它们提供了深度对象的副本。当对象具有 const 数据成员时,我将赋值运算符声明为私有,并且不实现它。
For the copy semantic I usually follow these rules:
1) if my class is a POD I can use the compiler generated copy constructor and assignment operator.
2) if I want to prevent copies of my object, I declare copy constructor and assignment operator private and I don't implement them
3) if my class is not a POD I provide my implementation of copy constructor and assignment operator that provide a deep copy of the object. When an object has const data members I declare the assignment operator private and I don't implement it.