C++ 中的可复制数据类

发布于 2024-11-16 22:51:14 字数 1079 浏览 3 评论 0原文

受到这篇文章的启发,我有一个包含嵌套可复制数据类的类。此可复制类对其成员类没有动态内存分配、保护或私有内存。

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 技术交流群。

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

发布评论

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

评论(3

坐在坟头思考人生 2024-11-23 22:51:14

您的CopyableDataClass就像一个值,类似于int。动态分配它并没有什么好处,就像您通常不会对单个 int 进行分配一样。

如果您的 Domain 类仅包含“可复制数据”,那么它将与其内容一样可复制。

Your CopyableDataClass is like a value, similar to an int. There is nothing much to gain from allocating it dynamically, just like you generally don't do that with single ints.

If your Domain class contains just "copyable data" it will be just as copyable as its content.

孤独患者 2024-11-23 22:51:14

您的用户类别应该如下所示:

class user
{
public:
    // we can't construct a domain-user without knowing it's domain
    user(Domain* domain) : m_domain(domain) { }

    // return the domain the user is part of
    Domain& get_domain() { return *this->m_domain; }

private:
    // you can't copy a domain-user
    user(user const&);
    user& operator=(user const&);

    Domain* m_domain;
};

Your user class should look like that:

class user
{
public:
    // we can't construct a domain-user without knowing it's domain
    user(Domain* domain) : m_domain(domain) { }

    // return the domain the user is part of
    Domain& get_domain() { return *this->m_domain; }

private:
    // you can't copy a domain-user
    user(user const&);
    user& operator=(user const&);

    Domain* m_domain;
};
梦在深巷 2024-11-23 22:51:14

对于复制语义,我通常遵循以下规则:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文