面向对象程序设计-C++_课时26拷贝构造Ⅰ_课时27拷贝构造Ⅱ

时间:2022-04-09 03:43:35

一旦写了一个类,给它3个函数:

1default construtor

2virtual destructor

3copy constructor

Constructions vs. assignment

Every object is constructed once

Every object should be destroyed once

--Failure to invoke delete()

--invoke delete() more than once

Once an object is constructed, it can be the target of many assignment operations

Copy ctor guidelines

In general, be explicit

--Create your own copy ctor--don't rely on the default

If you don't need one declare a private copy ctor

--prevents creation of a default copy constructor

--generates a compiler error if try to pass-by-value

--don't need a definition

复制构造函数,不是字节对字节的拷贝,而是成员对成员的拷贝

构造函数,参数是引用一个类

正确

 #include <iostream>
#include <string>
using namespace std; static int objectCount = ; class HowMany
{
public:
HowMany() { objectCount++; print("HowMany()"); }//构造函数,没有参数
HowMany(int i) { objectCount++; print("HowMany(int)"); }//构造函数,参数是一个int
HowMany(const HowMany& o) { objectCount++; print("HowMany(HM)"); }//构造函数,参数是引用一个类
void print(const string& msg = "")
{
if (msg.size() != )
{
std::cout << msg << ": ";
}
std::cout << "objectCount="
<< objectCount << std::endl;
}
~HowMany()
{
objectCount--;
print("~HowMany()");
}
}; HowMany f(HowMany x)
{
std::cout << "begin of f" << std::endl;
x.print("x argument inside f()");
std::cout << "end of f" << std::endl;
return x;
} void main()
{
HowMany h;
h.print("after construction of h"); HowMany h2 = f(h);
//HowMany h2 = h;
//HowMany h2(10);//构造函数,参数是一个int
//HowMany h2(h);//构造函数,参数是一个类 h.print("after call to f()"); system("pause");
}

构造函数,参数是新建一个类,形成死循环

错误

1>main.cpp(13): error C2652: “HowMany”: 非法的复制构造函数: 第一个参数不应是“HowMany”
1> main.cpp(8): note: 参见“HowMany”的声明
1>main.cpp(13): error C2333: “HowMany::HowMany”: 函数声明中有错误;跳过函数体
1>main.cpp(44): error C2440: “return”: 无法从“HowMany”转换为“HowMany”
1> main.cpp(44): note: 由于复制构造函数不明确或没有可用的复制构造函数,因此无法复制构造 class“HowMany”
1>main.cpp(52): error C2664: “HowMany f(HowMany)”: 无法将参数 1 从“HowMany”转换为“HowMany”
1> main.cpp(52): note: 由于复制构造函数不明确或没有可用的复制构造函数,因此无法复制构造 class“HowMany”

 #include <iostream>
#include <string>
using namespace std; static int objectCount = ; class HowMany
{
public:
HowMany() { objectCount++; print("HowMany()"); }//构造函数,没有参数
HowMany(int i) { objectCount++; print("HowMany(int)"); }//构造函数,参数是一个int HowMany(HowMany o) { objectCount++; print("HowMany(HM)"); }//构造函数,错误,参数是新建一个类,形成死循环 //1>main.cpp(13) : error C2652 : “HowMany” : 非法的复制构造函数 : 第一个参数不应是“HowMany”
// 1> main.cpp(8) : note : 参见“HowMany”的声明
// 1>main.cpp(13) : error C2333 : “HowMany::HowMany” : 函数声明中有错误;跳过函数体
// 1>main.cpp(44) : error C2440 : “return” : 无法从“HowMany”转换为“HowMany”
// 1> main.cpp(44) : note : 由于复制构造函数不明确或没有可用的复制构造函数,因此无法复制构造 class“HowMany”
// 1>main.cpp(52) : error C2664 : “HowMany f(HowMany)” : 无法将参数 1 从“HowMany”转换为“HowMany”
// 1> main.cpp(52) : note : 由于复制构造函数不明确或没有可用的复制构造函数,因此无法复制构造 class“HowMany” void print(const string& msg = "")
{
if (msg.size() != )
{
std::cout << msg << ": ";
}
std::cout << "objectCount="
<< objectCount << std::endl;
}
~HowMany()
{
objectCount--;
print("~HowMany()");
}
}; HowMany f(HowMany x)
{
std::cout << "begin of f" << std::endl;
x.print("x argument inside f()");
std::cout << "end of f" << std::endl;
return x;
} void main()
{
HowMany h;
h.print("after construction of h"); HowMany h2 = f(h);
//HowMany h2 = h;
//HowMany h2(10);//构造函数,参数是一个int
//HowMany h2(h);//构造函数,参数是一个类 h.print("after call to f()"); system("pause");
}

如果一个类有指针,需要另外写复制构造函数,否则出错

 #define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std; class Person
{
public:
Person(const char *s);
~Person();
void print();
char *name;
}; Person::Person(const char *s)
{
name = new char[::strlen(s) + ];
::strcpy(name, s);
} Person::~Person()
{
delete[] name;//array delete
} void Person::print()
{
std::cout << name << std::endl;
} void main()
{
Person p1("John");
Person p2(p1); p1.print();
p2.print(); printf("p1.name=%p\n", p1.name);//一样
printf("p2.name=%p\n", p2.name);//一样 system("pause");
}