关于C++中浅拷贝的问题
问题描述:
假设我有一个带有int指针成员变量“i”的struct“s”。我在s的缺省构造函数中为堆分配内存。后来在代码的其他部分中,我通过值将某个s的实例传递给某个函数。我在这里做一个浅拷贝吗?假设我没有实现任何复制构造函数或赋值运算符或任何s ...只是默认的构造函数。关于C++中浅拷贝的问题
答
为了跟进@ [don.neufeld.myopenid.com]的说法,它不仅是一个浅拷贝,而且可以是(选择)内存泄漏或悬挂指针。
// memory leak (note that the pointer is never deleted)
class A
{
B *_b;
public:
A()
: _b(new B)
{
}
};
// dangling ptr (who deletes the instance?)
class A
{
B *_b;
public:
A()
... (same as above)
~A()
{
delete _b;
}
};
要解决这个问题,有几种方法。
始终在使用原始内存指针的类中实现复制构造函数和operator =。
class A
{
B *_b;
public:
A()
... (same as above)
~A()
...
A(const A &rhs)
: _b(new B(rhs._b))
{
}
A &operator=(const A &rhs)
{
B *b=new B(rhs._b);
delete _b;
_b=b;
return *this;
};
不用说,这是一个重大的痛苦,有相当多的细微之处得到正确的。我甚至不确定我在这里做了什么,而且我已经做了几次。不要忘记你必须复制所有的成员 - 如果你稍后添加一些新成员,不要忘记添加它们!
在您的课堂上制作复制构造函数和operator = private。这是“锁门”解决方案。它简单而有效,但有时过度保护。
class A : public boost::noncopyable
{
...
};
切勿使用原始指针。这很简单有效。这里有很多的选择:
- 使用字符串类,而不是原始字符指针
- 使用std :: auto_ptr的,提高:: shared_ptr的,提高:: scoped_ptr的等
例子:
// uses shared_ptr - note that you don't need a copy constructor or op= -
// shared_ptr uses reference counting so the _b instance is shared and only
// deleted when the last reference is gone - admire the simplicity!
// it is almost exactly the same as the "memory leak" version, but there is no leak
class A
{
boost::shared_ptr<B> _b;
public:
A()
: _b(new B)
{
}
};
答
是的,这是一个浅的副本。你现在有两个s(调用者中的一个,堆栈中的一个作为参数),每个都包含一个指向同一块内存的指针。
答
您将有s
结构的两个副本,每一个都会有自己的i
指针,但两者i
指针将指向内存中的同一地址相同的值 - 所以,是的,这将是一个浅拷贝。
您的赋值运算符不是异常安全的。查看关于复制和异常安全的最新问题:http://stackoverflow.com/questions/214891/checklist-for-writing-copy-constuctor-and-assignment-operator-in-c#214966 – 2008-10-19 08:29:30