1. 程式人生 > >類陣列初始化的問題,以及複製建構函式const問題

類陣列初始化的問題,以及複製建構函式const問題

今天再編寫C++的作業時,又遇到一個問題,MyString SArray[4] = {“big”,“me”,“about”,“take”};這個的初始化始終出錯。起初我的理解是該語句的初始化是:SArray[0](“big”),也就是用一個字元陣列來初始化一個class MyString,但是我已經寫了這個建構函式啊,不應該出錯啊。然後在一步猜測,這個初始化可能是SArray[0](Mystring & str),但是也不對啊,我也寫了複製建構函式啊,然後百思不得其解,結果一下午又耗在這上了,唉,哭笑不得。
終於在3小時候發現了錯誤,原來我的複製建構函式寫成了Mystring( Mystring & str),正確應該是Mystring(const Mystring & str),因為這是一個const型實參,因此形參必須是const型,否則傳引數是要失敗的,因為const型的變數不能夠給變數來賦值(如果賦值了,變數一邊,const也變了,所以乾脆編譯時就禁止)。因為這一個小錯誤,浪費了3小時,唉,以後一定要注意,賦值建構函式的引數設成const型,可以確保正確。
但是以上題目我依舊有一點不大明白,也就是說為什麼類陣列初始化是用Mystring類來初始化的,而不是用字元陣列“big”這一些來初始化的。希望繼續學習後能解決此迷惑,或還請高手指點一二。

以下附上我的程式碼:

#include <cstdlib>
#include <iostream>
using namespace std;
int strlen(const char * s) 
{	int i = 0;
	for(; s[i]; ++i);
	return i;
}
void strcpy(char * d,const char * s)
{
	int i = 0;
	for( i = 0; s[i]; ++i)
		d[i] = s[i];
	d[i] = 0;
		
}
int strcmp(const char * s1,const char * s2)
{
	for(int i = 0; s1[i] && s2[i] ; ++i) {
		if( s1[i] < s2[i] )
			return -1;
		else if( s1[i] > s2[i])
			return 1;
	}
	return 0;
}
void strcat(char * d,const char * s)   //將字串s拼接到字串d的後面
{
	int len = strlen(d);
	strcpy(d+len,s);
}


class MyString
{
// your code
		char * p;
public:
	MyString(const char * s) {         //constructor function with a string
		if( s) {
			p = new char[strlen(s) + 1];
			strcpy(p,s);
		}
		else
			p = NULL;

	}


	/*MyString(char a[]) {         //constructor function with a *char
		if(a) {
			p = new char[strlen(a) + 1];
			strcpy(p,a);
		}
		else
			p = NULL;

	}*/


	MyString (const MyString & s)    //copy constructor function
	{
		if(s.p){
			p = new char[strlen(s.p+1)];
			strcpy(p,s.p); 
		}
		else 
			p = NULL;
	}


	MyString(){
		this->p = new char[1];
		*(this->p) = 0;
	}


	~MyString() { if(p) delete [] p; }


	void Copy( const char *s)
	{
		if (s){
			p = new char[strlen(s) + 1];
			strcpy(p,s);
		}
		else
			p = NULL;
	}


	MyString & operator= (const char*s)          //factor is the pointer
	{
		if (p)
			delete []p;
		if(s){
			p = new char[strlen(s)+1];
			strcpy(p,s);
		}
		else
			p = NULL;
		return *this;
	}


	MyString & operator= (const MyString & s)       //factor is the class MyString
	{
		if (p == s.p){
			return *this;
		}
		if(p)
			delete []p;
		if (s.p){
			p = new char [strlen(s.p)+1];
			strcpy(p,s.p);
		}
		else
			p = NULL;
		return *this;
	}


	char& operator[] (int n)
	{
		return this->p[n];
	}



	MyString operator+ (MyString s)   //reload the operator +
	{
		MyString temp;
		int length1 = strlen(this->p);
		int length2 = strlen(s.p);
		temp.p = new char[length1 + length2 + 1];
		strcpy(temp.p,this->p);
		strcat(temp.p,s.p);
		return temp;
	}



	MyString operator+ (const char* c)
	{
		MyString temp;
		int length = strlen(this->p);
		temp.p = new char[length+1];
		strcpy(temp.p,this->p);
		strcat(temp.p,c);
		return temp;
	}


	friend MyString operator+ (const char* str, MyString s)
	{
		MyString temp;
		int length1 = strlen(str);
		int length2 = strlen(s.p);
		temp.p = new char[length1 + length2 +1];
		strcpy(temp.p,str);
		strcat(temp.p,s.p);
		return temp;
	}


	MyString & operator+=(MyString s)
	{
		MyString temp;
		temp = *this;
		int length1 = strlen(temp.p);
		int length2 = strlen(s.p);
		delete []this->p;
		this->p = new char[length1 + length2 + 1];
		strcpy(this->p,temp.p);
		strcat(this->p,s.p);
		return *this;
	}


	MyString operator() (int m ,int n)
	{
		MyString temp;
		temp.p = new char[n+1];
		int i;
		for (i = m;i < m+n;i++){
			temp.p[i-m] = this->p[i];
		}
		temp.p[n] = 0;
		return temp;
	}


	friend  ostream & operator << (ostream & os,const MyString & s)
	{
		os<<s.p;
		return os;
	}



	int operator<(MyString str)
	{
		if(strcmp(this->p,str.p) == -1){
			return 1;
		}
		else return 0;
	}


	int operator>(MyString str)
	{
		if(strcmp(this->p,str.p) == 1){
			return 1;
		}
		else return 0;
	}
	
	
	int operator==(MyString str)
	{
		if(strcmp(this->p,str.p) == 0){
			return 1;
		}
		else return 0;
	}



};


int CompareString( const void * e1, const void * e2)
{
	MyString * s1 = (MyString * ) e1;
	MyString * s2 = (MyString * ) e2;
	if( * s1 < *s2 )
	return -1;
	else if( *s1 == *s2)
	return 0;
	else if( *s1 > *s2 )
	return 1;
}


int main()
{
	MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
	MyString SArray[4] = {"big","me","about","take"};
	cout << "1. " << s1 << s2 << s3<< s4<< endl;            //ok
	s4 = s3;                                            //ok
	s3 = s1 + s3;
	cout << "2. " << s1 << endl;
	cout << "3. " << s2 << endl;
	cout << "4. " << s3 << endl;
	cout << "5. " << s4 << endl;
	cout << "6. " << s1[2] << endl;
	s2 = s1;
	s1 = "ijkl-";
	s1[2] = 'A' ;
	cout << "7. " << s2 << endl;
	cout << "8. " << s1 << endl;
	s1 += "mnop";
	cout << "9. " << s1 << endl;
	s4 = "qrst-" + s2;
	cout << "10. " << s4 << endl;
	s1 = s2 + s4 + " uvw " + "xyz";
	cout << "11. " << s1 << endl;
	qsort(SArray,4,sizeof(MyString),CompareString);
	for( int i = 0;i < 4;i ++ )
		cout << SArray[i] << endl;
	//s1的從下標0開始長度為4的子串
	cout << s1(0,4) << endl;
	//s1的從下標5開始長度為10的子串
	cout << s1(5,10) << endl;
	return 0;
}