今天再编写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;
}