[問題] overload operator ==
看板C_and_CPP (C/C++)作者vic1225 (/* I'm not worth */)時間15年前 (2010/08/18 23:55)推噓1(1推 0噓 29→)留言30則, 5人參與討論串1/1
#include <iostream>
#include <stdlib.h>
using namespace std;
class String {
private:
char *str;
int length;
public:
//construct
String(int );
String(char *);
//copy construct
String(const String & );
//destuct
~String();
void reserve();
int lengthof();
String operator=(const String & );
char operator[](int );
String operator+(const String & );
//bool operator==(const String );
};
int main(){
String x("Iwillbe.");
String y(20);
String z(20);
cout << "Length of x is " << x.lengthof() << endl;
y = x;
cout << "Index 5 of y is " << y[5] << endl;
// cout << "Is x = y ? " << (x==y) << endl;
z = x + y;
cout << "Length of z is " << z.lengthof() << endl;
cout << "Index 11 of z is " << z[11] << endl;
system("pause");
return 0;
}
String::String(int n){
length = n;
if(length < 0){ cout << "Length < 0 is occured!" << endl; exit(-1); }
str = new char[length];
for(int i = 0; i < length; i++)
str[i] = 0;
}
String::String(char *a){
int n = 0;
while(a[n]!='\0') { n++; }
length = n;
if(length < 0){ cout << "Length < 0 is occured!" << endl; exit(-1); }
str = new char[length];
for(int i = 0; i < length; i++){
str[i] = a[i];
}
}
//copy construct
String::String(const String &a){
length = a.length;
if(length < 0){ cout << "Length < 0 is occured!" << endl; exit(-1); }
str = new char[length];
for(int i = 0; i < length; i++){
str[i] = a.str[i];
}
}
//destruct
String::~String(){
delete [] str;
length = 0;
}
String String::operator=(const String &a){
if(this != &a){
length = a.length;
if(length < 0){ cout << "Length < 0 is occured!" << endl; exit(-1); }
delete [] str;
str = new char[length];
for(int i = 0; i < length; i++){
str[i] = a.str[i];
}
}
else
length = 0;
return *this;
}
char String::operator[](int i){
if(length < 0){ cout << "Length < 0 is occured!" << endl; exit(-1); }
return str[i];
}
String String::operator+(const String &a){
if(a.length < 0 || length < 0){ cout << "Length < 0 is occured!" << endl;
exit(-1); }
char *c = new char[length];
for(int i = 0; i < length; i++){
c[i] = str[i];
}
length = a.length + length;
delete [] str;
str = new char[length];
for(int i = 0, j = 0; i < length; i++){
if(i < a.length){
str[i] = a.str[i];
}
else{
str[i] = c[j++];
}
}
return *this;
}
/*------------------------------------------------
bool operator==(const String a){
if(a.length != length) return false;
else{
for(int i = 0; i < length; i++){
if(a.str[i] != str[i]) return false;
}
}
return true;
}
---------------------------------------------------*/
int String::lengthof(){
return length;
}
註解掉的這段程式碼 編譯時他說要兩個參數
後來我改掉在編譯時他說要只能一個參數 = =
這是怎麼回事 高手煩請指教
謝謝!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.45.24.228
→
08/19 00:00, , 1F
08/19 00:00, 1F
→
08/19 00:01, , 2F
08/19 00:01, 2F
→
08/19 00:01, , 3F
08/19 00:01, 3F
→
08/19 00:11, , 4F
08/19 00:11, 4F
→
08/19 00:17, , 5F
08/19 00:17, 5F
→
08/19 00:22, , 6F
08/19 00:22, 6F
→
08/19 00:23, , 7F
08/19 00:23, 7F
→
08/19 00:23, , 8F
08/19 00:23, 8F
→
08/19 00:26, , 9F
08/19 00:26, 9F
→
08/19 00:27, , 10F
08/19 00:27, 10F
→
08/19 00:27, , 11F
08/19 00:27, 11F
→
08/19 00:27, , 12F
08/19 00:27, 12F
→
08/19 00:27, , 13F
08/19 00:27, 13F
→
08/19 00:27, , 14F
08/19 00:27, 14F
→
08/19 00:27, , 15F
08/19 00:27, 15F
→
08/19 00:29, , 16F
08/19 00:29, 16F
→
08/19 00:29, , 17F
08/19 00:29, 17F
→
08/19 00:29, , 18F
08/19 00:29, 18F
→
08/19 00:30, , 19F
08/19 00:30, 19F
推
08/19 00:30, , 20F
08/19 00:30, 20F
→
08/19 00:30, , 21F
08/19 00:30, 21F
→
08/19 00:30, , 22F
08/19 00:30, 22F
→
08/19 00:30, , 23F
08/19 00:30, 23F
→
08/19 00:31, , 24F
08/19 00:31, 24F
→
08/19 00:32, , 25F
08/19 00:32, 25F
→
08/19 00:32, , 26F
08/19 00:32, 26F
→
08/19 00:32, , 27F
08/19 00:32, 27F
→
08/19 00:33, , 28F
08/19 00:33, 28F
→
08/19 00:34, , 29F
08/19 00:34, 29F
→
08/19 00:36, , 30F
08/19 00:36, 30F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章