C++复制构造函数以及赋值运算符重载
//statement.h
#ifndef STATEMENT_H_
#define STATEMENT_H_
#include<iostream>
using std::cout;
using std::cin;
using std::ostream;
using std::istream;
class String
{
private:
char *str;
int len;
static int num;
public:
String();
String(const String &);//构造深度复制构造函数
String(const char *s);
~String();//对于用new创建的的构造函数来说,需要析构函数执行delete操作
int get_len(){ return len; }
static int get_num();//静态成员函数为整个类对象共享,只能调用静态数据成员
String& operator=(const char *s);
String& operator=(const String&);
char& operator[](const int i);
const char& operator[](const int i) const;//对于const对象
friend bool operator>(const String &str1, const String &str2);
friend bool operator<(const String &str1, const String &str2);
friend bool operator==(const String &str1, const String &str2);
friend String operator+(const String &str1,const String &str2);
friend ostream & operator<<(ostream &os, const String &str1);
friend istream& operator>>(istream &is, String &str1);
};
#endif //STATEMENT_H_
//define.cpp
/**
*Copyright U+00A9 2018 XXXXXXX. All Right Reserved.
*Filename:
*Author:XXXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/
#include<iostream>
#include<cstring>
//#include<cstdlib>
#include"statement.h"
#define MAXSIZE 100
using namespace std;
int String::num = 0;//单独赋值不属于任何一个对象
int String::get_num()
{
return num;
}
String::String()
{
len = 1;
str = new char[1];
str[0] = '\0';//为了配合delete[] str /*nullptr空指针*/
++num;
}
String::String(const String &s)
{
len = s.len;
str = new char[len + 1];
strcpy(str, s.str);
++num;
}
String::String(const char *s)
{
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
++num;
}
String::~String()
{
cout << "Target is release!\n";
delete[] str;
--num;
}
String& String::operator=(const char *s)
{
delete[]str;
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
return *this;
}
String& String::operator=(const String &s)
{
delete[] str;
len = s.len;
str = new char[len + 1];
strcpy(str, s.str);
return *this;
}
char& String::operator[](const int i)//因为返回char引用,因此可以str[i]='c'采用赋值去改变
{
return str[i];
}
const char& String::operator[](const int i) const
{
return str[i];
}
bool operator>(const String &str1, const String &str2)
{
return strcmp(str1.str, str2.str)>0;
}
bool operator<(const String &str1, const String &str2)
{
return !(str1>str2);
}
bool operator==(const String &str1, const String &str2)
{
return strcmp(str1.str, str2.str) == 0;
}
String operator+(const String &str1, const String &str2)
{
String temp;
temp.len = str1.len + str2.len + 1;
temp.str = new char[temp.len + 1];
strcpy(temp.str, str1.str);
strcat(temp.str, str2.str);
return temp;
}
ostream & operator<<(ostream &os, const String &str1)
{
os <<"*******"<< str1.str << "********"<<endl;
return os;
}
istream& operator>>(istream &is, String &str1)
{
is.getline(str1.str, MAXSIZE);
str1.len = strlen(str1.str);
return is;
}
//main.cpp
/**
*Copyright U+00A9 2018 XXXXXX. All Right Reserved.
*Filename:
*Author:XXXXXX
*Date:2018.12.01
*Version:VS 2013
*Description:
**/
#include<iostream>
//#include<cstdlib>
#include"statement.h"
using namespace std;
int main()
{
String str1 = "Hello,I'm a student";
String str2 = String("OK! I agree your comments");
cout << "str1=" << str1 << "str2=" << str2;
str1 = "I'm a chinese";
cout << "str1 = \"I'm a chinese\",str1=" << str1;
String str3;
cout << "please input a string for str3(MAXSIZE=100):" << endl;
cin >> str3;
cout << "str3=" << str3 << " ,*********its length=" << str3.get_len() << "**********\n";
String str4 = str1 + str2;
cout << "str1 + str2=str4=" << str4 << " ,*********its length=" << str4.get_len() << "**********\n";
cout << "output the greater between str1 and str2 is:" << endl;
if (str1 < str2) cout << "The greater is " << str2;
else cout << "The greater is " << str1;
String str5;
cout << "please input a string for str5(MAXSIZE=100):" << endl;
cin >> str5;
cout << "The 2-th element of str5 is " << str5[1] << endl;
str5[3] = '#';
cout << "str5[3] = '#'=> " << str5;
const String str6 = "I'm a student in NUAA";
cout << "The 3-th element of " << str6 << " is " << str6[2] << endl;
system("pause");
return 0;
}
对于数据成员含有指针的类,采用默认构造函数时只是按值进行赋值,这样将会使得被赋值的对象数据成员存取的是赋值对象的地址,因此,应当显式的构造复制构造函数去解决深度赋值问题。
程序运行结果