在C ++中复制构造函数和=运算符重载:是一个常见的函数吗? [英] Copy constructor and = operator overload in C++: is a common function possible?

查看:131
本文介绍了在C ++中复制构造函数和=运算符重载:是一个常见的函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从复制构造函数

  MyClass(const MyClass&); 

和an =运算符重载

  MyClass& operator =(const MyClass&); 

有相同的代码,相同的参数,有一个共同的功能,他们都使用?

解决方案

是的。有两个常见的选项。一个 - 我不推荐 - 是从复制构造函数中显式调用 operator =

  MyClass(const MyClass& other)
{
operator =(other);但是,提供一个好的 operator =



code>是一个挑战,当涉及到处理旧的状态和自我分配引起的问题。此外,所有成员和基址都将首先被初始化初始化,即使它们被分配给其他。这甚至可能对所有成员和基地都是无效的,即使它是有效的,它在语义上是多余的,并且可能实际上是昂贵的。



越来越流行的解决方案是实现 operator = 使用复制构造函数和交换方法。

  MyClass& operator =(const MyClass& other)
{
MyClass tmp(other);
swap(tmp);
return * this;
}

或甚至:

  MyClass& operator =(MyClass other)
{
swap(other);
return * this;
}

A swap 通常很容易写,因为它只是交换内部的所有权,不必清理现有的状态或分配新的资源。



复制和交换的优点成语是它自动地自动分配安全,并且 - 如果交换操作是不抛弃的 - 也是强异常安全的。



为了强烈的异常安全, 手书面指派运算符通常必须在重新分配受让人的旧资源之前分配新资源的副本,使得如果分配新资源时发生异常,则旧状态仍然可以返回。所有这些都是免费使用复制和交换,但通常是更复杂,因此容易出错,从头做。



一个要注意的是以确保交换方法是真正的交换,而不是使用复制构造函数和赋值运算符本身的默认 std :: swap



通常使用成员方法 swap std :: swap 工作,是所有基本类型和指针类型的无抛出保证。大多数智能指针也可以用无投保保证进行交换。


Since a copy constructor

MyClass(const MyClass&);

and an = operator overload

MyClass& operator = (const MyClass&);

have pretty much the same code, the same parameter, and only differ on the return, is it possible to have a common function for them both to use?

解决方案

Yes. There are two common options. One - which I don't recommend - is to call the operator= from the copy constructor explicitly:

MyClass(const MyClass& other)
{
    operator=(other);
}

However, providing a good operator= is a challenge when it comes to dealing with the old state and issues arising from self assignment. Also, all members and bases get default initialized first even if they are to be assigned to from other. This may not even be valid for all members and bases and even where it is valid it is semantically redundant and may be practically expensive.

An increasingly popular solution is to implement operator= using the copy constructor and a swap method.

MyClass& operator=(const MyClass& other)
{
    MyClass tmp(other);
    swap(tmp);
    return *this;
}

or even:

MyClass& operator=(MyClass other)
{
    swap(other);
    return *this;
}

A swap function is typically simple to write as it just swaps the ownership of the internals and doesn't have to clean up existing state or allocate new resources.

Advantages of the copy and swap idiom is that it is automatically self-assignment safe and - providing that the swap operation is no-throw - is also strongly exception safe.

To be strongly exception safe, a 'hand' written assignment operator typically has to allocate a copy of the new resources before de-allocating the assignee's old resources so that if an exception occurs allocating the new resources, the old state can still be returned to. All this comes for free with copy-and-swap but is typically more complex, and hence error prone, to do from scratch.

The one thing to be careful of is to make sure that the swap method is a true swap, and not the default std::swap which uses the copy constructor and assignment operator itself.

Typically a memberwise swap is used. std::swap works and is 'no-throw' guaranteed with all basic types and pointer types. Most smart pointers can also be swapped with a no-throw guarantee.

这篇关于在C ++中复制构造函数和=运算符重载:是一个常见的函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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