一.复制构造函数
1.用于将一个对象复制到新创建的对象中,注意初始化一词。也就是说,它用于初始化过程中,而不是常规的赋值中。
2.每当程序生成了对象副本时,编译器都将使用复制构造函数。具体的说,当函数按值传递对象或函数返回对象时,都将调用复制构造函数。
一个案例,源自《C++ Primer Plus》第12章课后习题的第2题。
当实例中打印copy时说明调用了复制构造函数,打印‘=’时说明调用了赋值运算符重载函数。
#include <iostream>
using namespace std;
#include "String.h"
int main()
{
String s1(" and I am a C++ student.");//String(const char *)构造函数
String s2 = "Please enter your name: ";//隐式类型转换,等价于String s2(const char *);不涉及复制构造
String s3;//默认构造函数
cout << s2;
cin >> s3;
s2 = "My name is " + s3;//"+"、"=" overload (const char * + String)
cout << s2<<".\n";
s2 = s2 + s1;//"+"overload String+String
s2.stringup();
cout << "The string\n" << s2 << "\ncontains " << s2.has('A')
<< " 'A' characters in it.\n";
s1 = "red";//等价于 s1 = String(const char *);
String rgb[3] = { String(s1)/*复制构造*/,String("green"),String("blue") };
cout << "Enter the name of a primary color for mixing light: ";
String ans;
bool success = false;
while (cin >> ans)
{
ans.stringlow();
for (int i = 0; i < 3; i++)
{
if (ans == rgb[i])//overload ==
{
cout << "That's right!\n";
success = true;
break;
}
}
if (success)
break;
else
cout << "Try again!\n";
}
cout << "Bye!\n";
while (1);
return 0;
}
函数的定义代码中详细描述了复制构造函数的调用时机。
#pragma warning(disable : 4996)
#include "String.h"
#include <string>
#include <iostream>
using namespace std;
String::String(const char * s)
{
//cout << "const char *";
len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
}
String::String(const String & st)
{
cout << "copy";
len = st.len;
str = new char[len + 1];
strcpy(str, st.str);
}
String::String()
{
len = 0;
str = new char[1];
str = '\0';
}
String& String::operator=(const String & st)
{
cout << "=";
if (this == &st) return *this;
delete[] str;
len = st.len;
str = new char[len + 1];
strcpy(str, st.str);
return *this;
}
ostream& operator<<(ostream& os,const String & st)
{
os << st.str;
return os;
}
istream& operator>>(istream& is, String & st)
{
char ch[80];
is.getline(ch, 80);
st.len = strlen(ch);
st.str = new char[st.len + 1];
strcpy(st.str, ch);
return is;
}
String operator+(const char * s, const String & st)
{
String temp;
temp.len = strlen(s) + st.len;
temp.str = new char[temp.len + 1];
strcpy(temp.str, s);
strcat(temp.str, st.str);
return temp;//返回局部变量,必须按值传递。按值传递,调用复制构造函数
}
String& String::operator+(const String & st)
{
len += st.len;
char temp[80];
strcpy(temp, str);
delete[] str;
str = new char[len + 1];
strcpy(str, temp);
strcat(str, st.str);
return *this;
}
void String::stringup()
{
for (int i = 0; i <= len; i++)
str[i] = toupper(str[i]);
}
int String::has(char ch)
{
int count = 0;
for (int i = 0; i <= len; i++)
if (str[i] == ch)
count++;
return count;
}
void String::stringlow()
{
for (int i = 0; i <= len; i++)
str[i] = tolower(str[i]);
}
bool String::operator==(const String & st)
{
if (len != st.len)
return false;
for (int i = 0; i <= len; i++)
if (str[i] != st.str[i])
return false;
return true;
}
String::~String()
{
delete[] str;
}