深拷贝与浅拷贝
一个类有指针成员
,如果在拷贝的时候顺带连指针指向的内存也分配了
,就称为深拷贝。如果只是分配指针本身的内存
,那就是浅拷贝
浅拷贝造成的问题是有两个指针指向同块内存,delete其中一个指针,那么剩下的指针将成为野指针
。编译器生成的默认拷贝构造函数和赋值运算符是浅拷贝的
#ifndef _STRING_H_
#define _STRING_H_
class String
{
public:
String(char *str = "");
~String();
String(const String &other);
String &operator=(const String &other);
void Display();
private:
char *AllocAndCpy(char *str);
char *str_;
};
#endif // _STRING_H_
#include "String.h"
//#include <string.h>
#include <cstring>
#include <iostream>
using namespace std;
String::String(char *str/* = */)
{
str_ = AllocAndCpy(str);
}
String::~String()
{
delete[] str_;
}
String::String(const String &other)
{
str_ = AllocAndCpy(other.str_);
}
String &String::operator =(const String &other)
{
if (this == &other)
return *this;
delete[] str_;
str_ = AllocAndCpy(other.str_);
return *this;
}
char *String::AllocAndCpy(char *str)
{
int len = strlen(str) + 1;
char *tmp = new char[len];
memset(tmp, 0, len);
strcpy(tmp, str);
return tmp;
}
void String::Display()
{
cout << str_ << endl;
}
#include "String.h"
int main(void)
{
String s1("AAA");
s1.Display();
String s2 = s1; // 调用拷贝构造函数
// 系统提供的默认拷贝构造函数实施的是浅拷贝 s2.str_ = s1.str_
String s3;
s3.Display();
s3 = s2; // 调用等号运算符
// 系统提供的默认等号运算符实施的是浅拷贝 s3.str_ = s2.str_;
// s3.operator=(s2);
s3.Display();
return 0;
}
如果我们想让对象是独一无二的,我们需要禁止拷贝。方法是将拷贝构造函数与=运算符声明为私有,并且不提供它们的实现
在编写派生类的赋值函数时,不要忘记对基类的数据成员重新赋值
,可以通过调用基类的赋值函数来实现。
Derived& Derived::operator=(const Derived& other) { }中调用Base::operator=(other);
空类与空数组
空类的大小为1个字节
,空类默认产生的成员:
class Empty {};
Empty(); // 默认构造函数
Empty( const Empty& ); // 默认拷贝构造函数
~Empty(); // 默认析构函数
Empty& operator=( const Empty& ); // 默认赋值运算符
Empty* operator&(); // 取址运算符
const Empty* operator&() const; // 取址运算符 const
体会一下下面程序
#include <iostream>
using namespace std;
class Empty
{
public:
Empty *operator&()
{
cout << "AAAA" << endl;
return this;
}
const Empty *operator&() const
{
cout << "BBBB" << endl;
return this;
}
};
int main(void)
{
Empty e;
Empty *p = &e; // 等价于e.operator&();
const Empty e2;
const Empty *p2 = &e2;
cout << sizeof(Empty) << endl;
return 0;
}
运行结果
AAAA
BBBB
1
空数组与空类
#include<iostream>
using namespace std;
int main()
{
int a[0];
class B {};
struct C
{
int m;
int n;
char buffer[];
};
class D
{
int s[0];
};
cout << "sizeof(a)=" << sizeof(a) << endl; //0
cout << "B{}=" << sizeof(B) << endl; //1
cout << "C=" << sizeof(C) << endl; //8
cout << "D=" << sizeof(D) << endl; //0
return 0;
}
运行结果
sizeof(a)=0
B{}=1
C=8
D=0