目录
深拷贝与浅拷贝
浅拷贝
String.h
#ifndef _STRING_H
#define _STRING_H
class String
{
public:
String(const char* str = nullptr);
~String();
void Display();
private:
char* str_;
};
#endif // _STRING_H
String.cpp
#include "String.h"
#include <string.h>
#include <iostream>
using namespace std;
String::String(const char* str)
{
int len = strlen(str) + 1;
str_ = new char[len];
memset(str_, 0, sizeof(str_));
strncpy_s(str_, sizeof(str_), str, sizeof(str));
}
String::~String()
{
delete [] str_;
}
void String::Display()
{
cout << str_ << endl;
}
main
#include "String.h"
int main()
{
String s1("AAA");
s1.Display();
String s2 = s1; // 调用默认的拷贝构造函数
// 系统提供的默认拷贝构造函数实施的是浅拷贝s2.str_ = s1.str_
s2.Display();
return 0;
}
运行结果:
深拷贝
String.h
#ifndef _STRING_H
#define _STRING_H
class String
{
public:
String(const char* str = nullptr);
String(const String& other); // xxxxxx
~String();
void Display();
private:
char* str_;
};
#endif // _STRING_H
String.cpp
#include "String.h"
#include <string.h>
#include <iostream>
using namespace std;
String::String(const char* str)
{
int len = strlen(str) + 1;
str_ = new char[len];
memset(str_, 0, sizeof(str_));
strncpy_s(str_, sizeof(str_), str, sizeof(str));
}
String::String(const String& other) // xxxxxx
{
int len = strlen(other.str_) + 1;
str_ = new char[len];
memset(str_, 0, sizeof(str_));
strncpy_s(str_, sizeof(str_), other.str_, sizeof(other.str_));
}
String::~String()
{
delete [] str_;
}
void String::Display()
{
cout << str_ << endl;
}
main
#include "String.h"
int main()
{
String s1("AAA");
s1.Display();
String s2 = s1; // 调用实现深拷贝的拷贝构造函数
s2.Display();
return 0;
}
运行结果:
赋值操作
深拷贝
String.h
#ifndef _STRING_H
#define _STRING_H
class String
{
public:
String(const char* str = "");
String(const String& other);
~String();
String& operator=(const String& other); // xxxxxx
void Display();
private:
char* str_;
};
#endif // _STRING_H
String.cpp
#include "String.h"
#include <string.h>
#include <iostream>
using namespace std;
String::String(const char* str)
{
int len = strlen(str) + 1;
str_ = new char[len];
memset(str_, 0, sizeof(str_));
strncpy_s(str_, sizeof(str_), str, sizeof(str));
}
String::String(const String& other)
{
int len = strlen(other.str_) + 1;
str_ = new char[len];
memset(str_, 0, sizeof(str_));
strncpy_s(str_, sizeof(str_), other.str_, sizeof(other.str_));
}
String::~String()
{
delete [] str_;
}
String& String::operator=(const String& other) // xxxxxx
{
if (this == &other)
return *this;
delete[] str_;
int len = strlen(other.str_) + 1;
str_ = new char[len];
memset(str_, 0, sizeof(str_));
strncpy_s(str_, sizeof(str_), other.str_, sizeof(other.str_));
return *this;
}
void String::Display()
{
cout << str_ << endl;
}
main
#include "String.h"
int main()
{
String s1("AAA");
s1.Display();
String s2 = s1; // 调用实现深拷贝的拷贝构造函数
s2.Display();
String s3; // xxxxxx
s3.Display(); // xxxxxx
s3 = s2; // 调用=运算符 // xxxxxx
// 系统提供的默认=运算符实施的是浅拷贝s3.str_ = s2.str_
s3.Display(); // xxxxxx
return 0;
}
禁止拷贝
将拷贝构造函数、等号运算符设为private;并且不提供其实现。
空类默认产生的成员
class Empty
{
Empty(); // 默认构造
Empty(const Empty& ); // 默认拷贝构造
~Empty(); // 默认析构
Empty& operator=(const Empty& ); // 默认赋值运算符
Empty* operator&(); // 取址运算符
const Empty* operator&() const; // 取址运算符const
};
1.默认构造
2.默认拷贝构造
3.默认析构
4.默认赋值运算符
5.取址运算符
6.取址运算符const
#include <iostream>
using namespace std;
class Empty
{
public:
Empty* operator&() // 取址运算符
{
return this;
}
};
int main()
{
Empty e;
Empty *p = &e;
cout << sizeof(Empty) << endl; // 输出1
return 0;
}