copyFrisky对象超出作用区域,
这时会调用复制构造函数创建该copyFrisky对象的一个临时拷贝对象,
并把它赋给主调函数中需要的对象,此时是成员复制;第二个例子可以证明!
然后调用该临时拷贝对象的析构函数释放这个对象占用的内存资源,
接着再调用形参对象的析构函数释放形参对象占用的内存资源!
以下是一个验证的例子:
//------------------------------------
//file name: testPlace.cpp
//purpose: to test copy constructor
//------------------------------------
#include <iostream.h>
class Cat
{
public:
Cat(); //default constructor
Cat(const Cat &); //copy constructor
~Cat(); //destructor
int GetAge() const {return itsAge;}
int GetWeight() const {return itsWeight;}
void SetAge(int age) {itsAge=age;}
void SetWeight(int weight) {itsWeight=weight;}
private:
int itsAge;
int itsWeight;
};
Cat::Cat() //constructor realization
{
itsAge=1;
itsWeight=10;
cout<<"constructor\n";
}
Cat::Cat(const Cat & theCat) //copy constructor realization
{
itsAge=theCat.GetAge();
itsWeight=theCat.GetWeight();
cout<<"copy constructor\n";
}
Cat::~Cat() //destructor realization
{
itsAge=0;
itsWeight=0;
cout<<"destructor\n";
}
Cat DisplayCopyFrisky(Cat); //subfuction header
int main() //main function
{
Cat Frisky,copyFrisky;
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
Frisky.SetAge(2);
copyFrisky=DisplayCopyFrisky(Frisky); //call subfuction
cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl;
Frisky.SetAge(3);
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
return 0;
}
Cat DisplayCopyFrisky(Cat copyFrisky) //subfuction realization
{
cout<<"copyFrisky' age: "<<copyFrisky.GetAge() <<endl;
cout<<"copyFrisky' weight: "<<copyFrisky.GetWeight() <<endl;
copyFrisky.SetAge(4);
cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl;
return copyFrisky;
}
证明返回对象的值时是成员复制的第二个例子:由于返回对象值时采用的是浅复制,而对象中含有指向自由存储区的指针变量,从而导致临时拷贝对象中的指针变量和接受返回值的对象(在此设为:copyFrisky)的指针变量同时指向同一内存块,而当浅复制完毕后,析构函数释放临时拷贝对象的内存资源,导致接受返回值的对象copyFrisky中的指针变量变成迷途指针,返回到主程序之后,在主程序结束前,应该先把copyFrisky中的迷途指针设为空指针或者指向某一分配的内存,否则当主程序结束调用析构函数释放copyFrisky对象的资源时,将会导致错误!正如第二个例子所示!改正后的程序如第三个例子!
//------------------------------------
//file name: testPlace.cpp
//purpose: to test copy constructor
//------------------------------------
#include <iostream.h>
class Cat
{
public:
Cat(); //default constructor
Cat(const Cat &); //copy constructor
~Cat(); //destructor
int GetAge() const {return *itsAge;}
int GetWeight() const {return *itsWeight;}
int* GetItsAgeValue() const {return itsAge;}
int* GetItsWeightValue() const {return itsWeight;}
void DeleteAge() {delete itsAge;itsAge=0;}
void DeleteWeight() {delete itsWeight;itsWeight=0;}
void SetAge(int age) {*itsAge=age;}
void SetWeight(int weight) {*itsWeight=weight;}
private:
int *itsAge;
int *itsWeight;
};
Cat::Cat() //constructor realization
{
itsAge=new int;
itsWeight=new int;
*itsAge=1;
*itsWeight=10;
cout<<"constructor\n";
}
Cat::Cat(const Cat & theCat) //copy constructor realization
{
itsAge=new int;
itsWeight=new int;
*itsAge=theCat.GetAge();
*itsWeight=theCat.GetWeight();
cout<<"copy constructor\n";
}
Cat::~Cat() //destructor realization
{
delete itsAge;
itsAge=0;
delete itsWeight;
itsWeight=0;
cout<<"destructor\n";
}
Cat DisplayCopyFrisky(Cat); //subfuction header
int main() //main function
{
Cat Frisky,copyFrisky;
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
Frisky.SetAge(2);
copyFrisky.DeleteAge();
copyFrisky.DeleteWeight();
copyFrisky=DisplayCopyFrisky(Frisky); //call subfuction
cout<<"copyFrisky's age: "<<copyFrisky.GetItsAgeValue() <<endl;
cout<<"copyFrisky's weight: "<<copyFrisky.GetItsWeightValue() <<endl;
cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl; //lost pointer
cout<<"copyFrisky's weight: "<<copyFrisky.GetWeight() <<endl; //lost pointer
Frisky.SetAge(3);
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
return 0;
}
Cat DisplayCopyFrisky(Cat copyFrisky) //subfuction realization
{
cout<<"copyFrisky's age: "<<copyFrisky.GetItsAgeValue() <<endl;
cout<<"copyFrisky's weight: "<<copyFrisky.GetItsWeightValue() <<endl;
copyFrisky.SetAge(4);
cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl;
return copyFrisky;
}
第三个例子:这个例子返回的是迷途指针,此目的只是想了解返回对象值时的一些情况!
//------------------------------------
//file name: testPlace.cpp
//purpose: to test copy constructor
//------------------------------------
#include <iostream.h>
class Cat
{
public:
Cat(); //default constructor
Cat(const Cat &); //copy constructor
~Cat(); //destructor
int GetAge() const {return *itsAge;}
int GetWeight() const {return *itsWeight;}
int* GetItsAgeValue() const {return itsAge;}
int* GetItsWeightValue() const {return itsWeight;}
void DeleteAge() {delete itsAge;itsAge=0;}
void DeleteWeight() {delete itsWeight;itsWeight=0;}
void SetAgeEmpty() {itsAge=0;}
void SetWeightEmpty() {itsWeight=0;}
void SetAge(int age) {*itsAge=age;}
void SetWeight(int weight) {*itsWeight=weight;}
private:
int *itsAge;
int *itsWeight;
};
Cat::Cat() //constructor realization
{
itsAge=new int;
itsWeight=new int;
*itsAge=1;
*itsWeight=10;
cout<<"constructor\n";
}
Cat::Cat(const Cat & theCat) //copy constructor realization
{
itsAge=new int;
itsWeight=new int;
*itsAge=theCat.GetAge();
*itsWeight=theCat.GetWeight();
cout<<"copy constructor\n";
}
Cat::~Cat() //destructor realization
{
delete itsAge;
itsAge=0;
delete itsWeight;
itsWeight=0;
cout<<"destructor\n";
}
Cat DisplayCopyFrisky(Cat); //subfuction header
int main() //main function
{
Cat Frisky,copyFrisky;
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
Frisky.SetAge(2);
copyFrisky.DeleteAge();
copyFrisky.DeleteWeight();
copyFrisky=DisplayCopyFrisky(Frisky); //call subfuction
copyFrisky.SetAgeEmpty();
copyFrisky.SetWeightEmpty();
cout<<"copyFrisky's age: "<<copyFrisky.GetItsAgeValue() <<endl;
cout<<"copyFrisky's weight: "<<copyFrisky.GetItsWeightValue() <<endl;
//cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl; //lost pointer
//cout<<"copyFrisky's weight: "<<copyFrisky.GetWeight() <<endl; //lost pointer
Frisky.SetAge(3);
cout<<"Frisky's age: "<<Frisky.GetAge() <<endl;
return 0;
}
Cat DisplayCopyFrisky(Cat copyFrisky) //subfuction realization
{
cout<<"copyFrisky's age: "<<copyFrisky.GetItsAgeValue() <<endl;
cout<<"copyFrisky's weight: "<<copyFrisky.GetItsWeightValue() <<endl;
copyFrisky.SetAge(4);
cout<<"copyFrisky's age: "<<copyFrisky.GetAge() <<endl;
return copyFrisky;
}