//======================================
//===========拷貝構(gòu)造函數(shù)的重載=========
//======================================
//======================================
//======================================
兩個(gè)指針成員指向默認(rèn)拷貝構(gòu)造函數(shù)缠借,會(huì)出現(xiàn)地址錯(cuò)誤郎哭。
include <iostream>
using namespace std;
class A{
int date;
public:
A(int d) :date(d){
cout << "A(int)"<< endl;
}
A(char d) :date(d){
cout << "A(char)"<< endl;
}
A(bool d) :date(d ? 123:345){
底循、cout << "A(bool)"<< endl;
}
A(int& d) :date(d){
cout << "A(int&)"<< endl;
}
A(const A& o) :date(o.date){
cout << "A(const &)"<< endl;
}
/* A(const A o) :date(o.date){
cout << "A(const &)"<< endl;
}
*///將引用去掉,這樣行不行混弥。。不行不从!為什么买雾?引用是用來初始化變量的別名。傳入的數(shù)據(jù)進(jìn)行初始化剪撬,非引用會(huì)將實(shí)參復(fù)制一部分給形參摄乒,此時(shí)的形參是新變量,由實(shí)參進(jìn)行初始化
//如果沒有引用残黑,A a2(a1),調(diào)用A(A o). A o (a1)..相當(dāng)于反復(fù)的創(chuàng)建a1馍佑,因此不行。(創(chuàng)建對象時(shí)梨水,要調(diào)用構(gòu)造函數(shù)拭荤。調(diào)用構(gòu)造函數(shù)時(shí),又要?jiǎng)?chuàng)建形參疫诽,而形參是新的對象舅世,而創(chuàng)建新對象有要調(diào)用構(gòu)造函數(shù),調(diào)用構(gòu)造函數(shù)需要形參奇徒,這樣反反復(fù)復(fù)編譯器編譯不能成功)
virtual ~A(){
cout <<"~A()" <<endl; //一個(gè)類中存在一個(gè)析構(gòu)函數(shù)
}
void show()const {
cout << "date="<< date<<endl;
}
};
void show(A obj)
{
obj.show(); //拷貝構(gòu)造函數(shù)的使用
}
void func(const A& obj)
{
obj.show(); //注意傳入?yún)?shù)為常量歇终,因此在類中的show函數(shù)中,需要定義為常量
}
int main(int argc,char argv[])
{
//A a1(3);
show('v'); //調(diào)用類外的show函數(shù)逼龟,并定義A obj為char 類型的字符串评凝。。用過之后直接析構(gòu)掉了腺律。奕短。
show(23);
A a1(3);
show(a1);
//A a2(true);
//A& a3 = a1;
//a3.show();
//show(a3);
//A a4(a1); //這個(gè)是采用A(const A& o )
//A a5('V');
//A a6();
cout <<"======func========" << endl;
func(a1); //因?yàn)槭褂靡茫虼嗽谝呀?jīng)有a1的情況下匀钧,不會(huì)再創(chuàng)建新對象
}
/*
//動(dòng)態(tài)數(shù)組類翎碑,可變長短的數(shù)組。之斯。
include <iostream>
include <string>
using namespace std;
class A{
char* p; //因?yàn)槭莿?dòng)態(tài)數(shù)組日杈,不能直接定義數(shù)組。
int len;
public:
A(int n) : len(0),p(NULL) { //構(gòu)造函數(shù),設(shè)置n個(gè)數(shù)組長度
resize(n);
}
void resize(int n){ //作用莉擒,將數(shù)組長度調(diào)整為指定長度
char* q = new char[n]; //新傳入數(shù)組酿炸,設(shè)定長度
int min = ( n< len? n:len );
if(p!=NULL)
{
for(int i=0; i< min; i++)
{ //p指向的數(shù)據(jù)全部轉(zhuǎn)移到q指向的空間
q[i] = p[i];
}
delete []p;
p = q;
for(i = min; i<n; i++)
p[i] = '\0';
}
len = n;
}
void set(int index,char value)
{
if(index>0)
p[index] = value;
}
char get(int index){
return p[index];
}
~A(){
if(p!=NULL){
delete[] p;
}
}
A (const A& o): len(o.len){
p = new char [len];
for(int i=0; i<len; i++)
p[i] = o.p[i];
} //拷貝構(gòu)造函數(shù),此算法是深拷貝
};
int main()
{
A a1(10);
a
}
//此方法是淺顯的拷貝
*/
include <iostream>
using namespace std;
class Array{
char* p;
int len;
public:
Array(int n) : len(n),p(new char[n]){}
int size(){
return len;
}
~Array(){
delete []p;
}
void fill(char start,int skip)
{
for(int i = 0;i<len ;i++)
p[i]=start +i*skip;
}
void show(){
for(int i=0; i< len; i++ )
cout << p[i];
cout << endl;
}
Array(const Array& o) :len(o.len){
p = new char [len];
for(int i=0; i<len; i++)
p[i] = o.p[i];
}
};
int main()
{
Array a1(10);
a1.fill('a',1);
a1.show();
Array a2(a1);
a2.fill('A',1);
a2.show();
a1.show();
}