如何使用复制构造函数和复制赋值都是私有的对象?

发布于 2024-09-04 06:57:34 字数 475 浏览 2 评论 0原文

在阅读TCPL时,我遇到了一个问题,正如标题所提到的,然后'私有'类是:

class Unique_handle {
private:
    Unique_handle& operator=(const Unique_handle &rhs);
    Unique_handle(const Unique_handle &rhs);
public:
    //...
};

使用代码是:

struct Y {
    //...
    Unique_handle obj;
};

并且我想执行这样的操作:

int main()
{
    Y y1;
    Y y2 = y1;
}

虽然,这些代码来自TCPL ,但我仍然无法得到解决方案...... 有人可以帮助我吗,不胜感激。

In reading TCPL, I got a problem, as the title refered, and then 'private' class is:

class Unique_handle {
private:
    Unique_handle& operator=(const Unique_handle &rhs);
    Unique_handle(const Unique_handle &rhs);
public:
    //...
};

the using code is:

struct Y {
    //...
    Unique_handle obj;
};

and I want to execute such operations:

int main()
{
    Y y1;
    Y y2 = y1;
}

although, these code are come from TCPL, but I still can not got the solution...
Can anybody help me, appreciate.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

街道布景 2024-09-11 06:57:34

顾名思义,Unique_handle 并不是用来复制的。它的实现通过禁用复制构造函数和复制赋值运算符来确保这一点。

对于有权访问Unique_handle的多个实例的一种解决方案是保存指向它的指针,并复制该指针。然后 Y 的多个实例指向同一个唯一句柄。

但是,在这种情况下,请注意正确管理资源。

As its name suggests, the Unique_handle isn't meant to be copied. Its implementation ensures it by disabling the copy constructor and copy assignment operator.

One solution for multiple instances having access to a Unique_handle is holding a pointer to it, and copying the pointer. Then multiple instances of Y point to the same unique handle.

Take care, however, to manage resources properly in this case.

二智少女猫性小仙女 2024-09-11 06:57:34

您在 Stroustrup 的书中看到的示例演示了类的设计者如何显式阻止对该类的对象进行复制或赋值。

该类是故意这样做的,这样您的代码就无法执行您想要执行的操作(可能是因为该类无法正常运行,或者复制没有意义)。如果您希望能够复制该类的对象,则需要重新设计该类。

您可能有一些其他选项(这也可能没有意义,但这取决于您真正在做什么) - 传递指向对象引用的指针而不是复制。

The example you're looking at in Stroustrup's book is demonstrating how the designer of a class can explicitly prevent copying or assignment of objects of that class.

The class is intentionally making it so your code can't do what you're trying to do (presumably because the class won't function properly or copying otherwise doesn't make sense). If you want to be able to copy objects of that class, you'll need to redesign the class.

Some other options you might have (which also might not make sense, but that depends on what your really doing) - pass around pointers to references to the object instead of copying.

枫以 2024-09-11 06:57:34

通常,将复制构造函数和赋值运算符设为私有(且未实现)的习惯用法意味着该类的原始作者明确不希望该对象可复制。

Usually, the idiom of making your copy constructor and assignment operator private (and unimplemented) implies that the original author of the class specifically did not want this object to be copyable.

怂人 2024-09-11 06:57:34

你不应该尝试复制它。但话虽如此......你可以memcpy它。你这样做的唯一原因是你知道自己在做什么,你知道后果等等。它通常会超出一般可接受的范围。但你可能想做一些忍者的事情。

you shouldn't try to copy it. But having said that.... you can memcpy it. Only reason you'd do that is that you know what your doing, you know the consequences, etc etc. Its generally stepping out of the bounds of whats generally acceptable. But you might want to do something a bit ninja.

偏闹i 2024-09-11 06:57:34

我用谷歌搜索了我的问题,并找到了一种构造这样一个对象的方法:

static Unique_handle* instance() { return new Unique_handle();但这

似乎是错误的,那么我如何在外部定义这样的对象呢?

不管怎样,谢谢大家的关心。

I have googled my question, and find a way to construct such a object:

static Unique_handle* instance() { return new Unique_handle(); }

but it seems wrong, then how can I define such a object outside?

Anyway, thank you for all of your concern.

讽刺将军 2024-09-11 06:57:34

您可以使用 shared_ptr 来共享对象:

class Y
{
public:
  Y(): mHandle(new UniqueHandle()) {}

private:
  boost::shared_ptr<UniqueHandle> mHandle;
};

就这么简单。

如果您不想共享所有权,则可以使用 boost::scoped_ptr 或新创建的 std::unique_ptr(如果您有权访问它),然后实现 CopyConstructor和AssignmentOperator你自己,照顾他们的语义。

You can use a shared_ptr to share the object:

class Y
{
public:
  Y(): mHandle(new UniqueHandle()) {}

private:
  boost::shared_ptr<UniqueHandle> mHandle;
};

It's as simple as that.

If you don't want shared ownership, you can use boost::scoped_ptr or the newly created std::unique_ptr if you have access to it, and then implement the CopyConstructor and AssignmentOperator yourself, taking care of their semantics.

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