#include <iostream> #include <string.h> #include <stdlib.h> using namespace std; class myStrcmp { public: //在声明处默认参数 myStrcmp(char *str=NULL); //析构器 ~myStrcmp() { cout<<"caibi"<<endl; delete []str; } //运算符重载 int operator > (myStrcmp &str2); int operator < (myStrcmp &str2); int operator = (myStrcmp &str2); private: char *str; //成员函数str存放字符串 }; //构造器 myStrcmp::myStrcmp(char *str) { //如果没有值传进来,字符串置空 if(str==NULL) { this->str =new char[1]; *this->str=' '; } //如果有值传入,使用strcpy拷贝进对象的str中 else { this->str =new char[strlen(str)+1]; //申请空间 strcpy(this->str,str); } } //strcmp运算符重载与自实现 int myStrcmp::operator > (myStrcmp &str2) { //字符比较,不等退出 char *p=str2.str; //临时指针变量p,合理调用析构器析构str2 for(;*this->str == *p && *p!= ' ' && *this->str != ' ';(this->str)++,p++); //比较退出时第一个字符大小 if(*this->str > *p) return 1; } int myStrcmp::operator < (myStrcmp &str2) { //字符比较,不等退出 char *p=str2.str; //临时指针变量p for(;*this->str == *p && *p != ' ' && *this->str != ' ';(this->str)++,p++); //比较退出时第一个字符大小 if(*this->str < *p) return -1; } int myStrcmp::operator = (myStrcmp &str2) { //字符比较,不等退出 char *p=str2.str; //临时指针变量p for(;*this->str == *p && *p != ' ' && *this->str != ' ';(this->str)++,p++); //比较退出时第一个字符大小 if((*this->str) == (*p)) return 0; } int main() { myStrcmp str1("panda"); //字符串s1 myStrcmp str2("panmsa"); //字符串s2 //判断str1与str2大小 if(str1>str2==1) cout<<"str1>str2"<<endl; else if(str1<str2==-1) cout<<"str1<str2"<<endl; else if((str1=str2)==0) cout<<"str1=str2"<<endl; system("pause"); return 0; }
总结:析构器在析构时,若未定义str2的临时指针变量,str2会析构错误;不要改变指针位置,使用下标进行比较
#include <iostream> #include <string.h> #include <stdlib.h> using namespace std; class myStrcmp { public: //在声明处默认参数 myStrcmp(char *str=NULL); //析构器 ~myStrcmp() { cout<<"caibi"<<endl; delete []str; } //运算符重载 int operator > (myStrcmp &str2); int operator < (myStrcmp &str2); int operator = (myStrcmp &str2); private: char *str; //成员函数str存放字符串 }; //构造器 myStrcmp::myStrcmp(char *str) { //如果没有值传进来,字符串置空 if(str==NULL) { this->str =new char[1]; *this->str=' '; } //如果有值传入,使用strcpy拷贝进对象的str中 else { this->str =new char[strlen(str)+1]; //申请空间 strcpy(this->str,str); } } //strcmp运算符重载与自实现 int myStrcmp::operator > (myStrcmp &str2) { //字符比较,不等退出 int i=0; for(;this->str[i] == str2.str[i] && str2.str[i]!= ' ' && this->str[i] != ' ';i++); //比较退出时第一个字符大小 if(this->str[i] > str2.str[i]) return 1; } int myStrcmp::operator < (myStrcmp &str2) { //字符比较,不等退出 int i=0; for(;this->str[i] == str2.str[i] && str2.str[i]!= ' ' && this->str[i] != ' ';i++); //比较退出时第一个字符大小 if(this->str[i] < str2.str[i]) return -1; } int myStrcmp::operator = (myStrcmp &str2) { //字符比较,不等退出 int i=0; for(;this->str[i] == str2.str[i] && str2.str[i]!= ' ' && this->str[i] != ' ';i++); //比较退出时第一个字符大小 if((this->str[i]) == (str2.str[i])) return 0; } int main() { myStrcmp str1("panda"); //字符串s1 myStrcmp str2("pandaa"); //字符串s2 //判断str1与str2大小 if(str1>str2==1) cout<<"str1>str2"<<endl; else if(str1<str2==-1) cout<<"str1<str2"<<endl; else if((str1=str2)==0) cout<<"str1=str2"<<endl; system("pause"); return 0; }