我可以在operator =中使用placement new(this)吗? [英] Can I use placement new(this) in operator=?

查看:167
本文介绍了我可以在operator =中使用placement new(this)吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
我有一个包含许多变量的复杂类。我有一个健全和测试的复制构造函数:

  Applepie :: Applepie(const Applepie& copy):
m_crust (copy.m_crust),
m_filling(copy.m_filling)
{
}

在初始化列表中调用的一些成员变量复制构造函数执行分配。



问题:创建 operator = 。而不是用赋值而不是初始化列表来重复现有的引用,并释放正被替换的内存,等等,我可以简单地做以下:

  Applepie& Applepie :: operator =(const Applepie& copy)
{
if(this!=& copy)
{
this-> //释放自己的对象
new(this)Applepie(copy); // placement new copy constructor
}
return * this;
}

换句话说,就是destroy self后跟一个放置新的拷贝构造函数语义相同to operator =?



这似乎有可能大大减少重复代码,并确认每个变量被正确初始化,代价是分配期间潜在的轻微效率损失。


我的实际类有大约30个varaibles。我担心的事实,我的复制构造函数和我的赋值运算符必须复制所有三十,并且代码可能分歧,导致两个操作做不同的事情。

解决方案

由于Herb Sutter在异常C ++状态,它不是异常安全。这意味着,如果在 new 或构造新对象期间出现任何错误,分配的左手操作数处于坏(未定义)状态,这就要求更多的麻烦。我强烈建议您使用复制& swap成语

  Applepie& Applepie :: operator =(Applepie copy)
{
swap(m_crust,copy.m_crust);
swap(m_filling,copy.m_filling);
return * this;
}

当您的对象使用 pimpl idiom 到实现),交换只通过改变两个指针来完成。


Background: I have a complicated class with many variables. I have a sound and tested copy constructor:

Applepie::Applepie( const Applepie &copy) :
m_crust(copy.m_crust),
m_filling(copy.m_filling)
{
}

Some of the member variable copy constructors called in the intializer list perform allocation.

Question: I need to create operator=. Rather than duplicating the existing constuctor with assignment instead of initialization list, and freeing memory that's being replaced, and etc etc etc, can I simply do the following:

Applepie& Applepie::operator=( const Applepie &copy)
{
    if( this != &copy)
    {
       this->~Applepie(); // release own object
       new(this) Applepie(copy); // placement new copy constructor
    }
    return *this;
}

In other words, is destroy self followed by a placement new copy constructor semantically identical to operator= ?

This seems to have the potential to dramatically reduce repeat code and confirming that each variable is initialized properly, at the cost of potential slight loss of efficiency during assignment. Am I missing something more obscure?

Rationale: My actual class has about 30 varaibles. I am concerned about the fact that both my copy constructor and my assignment operator have to copy all thirty, and that the code might diverge, causing the two operations to do things differently.

解决方案

As Herb Sutter in "Exceptional C++" states, it is not exception safe. That means, if anything is going wrong during new or construction of the new object, the left hand operand of the assignment is in bad (undefined) state, calling for more trouble. I would strongly recommend using the copy & swap idiom.

Applepie& Applepie::operator=(Applepie copy)
{
  swap(m_crust, copy.m_crust);
  swap(m_filling, copy.m_filling);
  return *this;
}

When your object uses the Pimpl idiom (pointer to implementation) also, the swap is done by changing only two pointers.

这篇关于我可以在operator =中使用placement new(this)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆