复制构造函数, 深复制与浅复制
浅复制
#include<bits/stdc++.h>
using namespace std;
class StringTest
{
private:
char *str;
int len;
static int strings_count;
public:
StringTest(const char *s);//构造函数
~StringTest() ;
void address();
friend ostream & operator << (ostream & os, const StringTest & st);
};
// 设置strings_count 的初始值
int StringTest::strings_count=0;
StringTest::StringTest(const char *s)
{
len=strlen(s);
str = new char[len+1];
strcpy(str, s);
strings_count++;
cout<<"#"<<strings_count<<": \""<<str<<" \"对象已生成"<<endl;
}
StringTest::~StringTest()
{
cout<<"\""<<str<<"\" object delete, ";
strings_count--;
cout<<strings_count<<" left"<<endl;
delete [] str;
}
void StringTest::address()
{
printf("%08X\n", str);
}
ostream & operator << (ostream & os, const StringTest & st)
{
os<<st.str;
return os;
}
int main()
{
StringTest str1("Tabletennis");
cout<<str1<<endl;
StringTest str2 = str1;
cout<<str2<<endl;
//从结果可以看出两个字符串是指向的同一个位置, 这是默认的复制构造函数产生的结果
str1.address();
str2.address();
printf("%0X\n", &str1);
printf("%0X\n", &str2);
cout<<sizeof(StringTest)<<endl;
return 0;
}
结果:
#1: "Tabletennis "对象已生成
Tabletennis
Tabletennis
001D68D0
001D68D0
6FFE00
6FFDF0
16
"Tabletennis" object delete, 0 left
"Tabletennis" object delete, -1 left
深复制
#include<bits/stdc++.h>
using namespace std;
class StringTest
{
private:
char *str;
int len;
static int strings_count;
public:
StringTest(const char *s);//构造函数
StringTest(const StringTest & st);//复制构造函数
StringTest() ;//默认构造函数
~StringTest() ;
void address();
friend ostream & operator << (ostream & os, const StringTest & st);
};
// 设置strings_count 的初始值
int StringTest::strings_count=0;
StringTest::StringTest(const char *s)
{
len=strlen(s);
str = new char[len+1];
strcpy(str, s);
strings_count++;
cout<<"#"<<strings_count<<": \""<<str<<"\" 对象已生成 by 构造函数"<<endl;
}
StringTest::StringTest()
{
len = 4;
str = new char[30];
strcpy(str, "Baseball");
strings_count++;
cout<<"#"<<strings_count<<": \""<<str<<"\" 对象已生成 by 默认构造函数"<<endl;
}
StringTest::StringTest(const StringTest & st)
{
len = st.len;
str = new char[len+1];
strcpy(str, st.str);
strings_count++;
cout<<"#"<<strings_count<<": \""<<str<<"\" 对象已生成 by 复制构造函数"<<endl;
}
StringTest::~StringTest()
{
cout<<"\""<<str<<"\" object delete, ";
strings_count--;
cout<<strings_count<<" left"<<endl;
delete [] str;
}
void StringTest::address()
{
printf("%08X\n", str);
}
ostream & operator << (ostream & os, const StringTest & st)
{
os<<st.str;
return os;
}
int main()
{
StringTest str1("Tabletennis");
cout<<str1<<endl;
StringTest str2;
cout<<str2<<endl;
StringTest str3=str1;
cout<<str3<<endl;
//从结果可以看出三个字符串的地址都不一样,这次是深拷贝
str1.address();
str2.address();
str3.address();
printf("%0X\n", &str1);
printf("%0X\n", &str2);
printf("%0X\n", &str3);
cout<<sizeof(StringTest)<<endl;
return 0;
}
结果:
#1: "Tabletennis" 对象已生成 by 构造函数
Tabletennis
#2: "Baseball" 对象已生成 by 默认构造函数
Baseball
#3: "Tabletennis" 对象已生成 by 复制构造函数
Tabletennis
00B71510
00B71530
00B71A10
6FFE00
6FFDF0
6FFDE0
16
"Tabletennis" object delete, 2 left
"Baseball" object delete, 1 left
"Tabletennis" object delete, 0 left