[問題] overload operator ==

看板C_and_CPP (C/C++)作者 (/* I'm not worth */)時間15年前 (2010/08/18 23:55), 編輯推噓1(1029)
留言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
== 是二元運算子, 所以一定要兩個參數, 寫成member
08/19 00:00, 1F

08/19 00:01, , 2F
function 時也沒例外, 只是第一個參數內定是 *this
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
String(char const *);
08/19 00:17, 5F

08/19 00:22, , 6F
bool String::operator==(const String & a) 最快的
08/19 00:22, 6F

08/19 00:23, , 7F
方法是寫成member function, 注意類別定義也要加上宣
08/19 00:23, 7F

08/19 00:23, , 8F
08/19 00:23, 8F

08/19 00:26, , 9F
我像你說的這樣改 編譯時說:OPERATOR==NEED TWO AUGMENT
08/19 00:26, 9F

08/19 00:27, , 10F
寫成member function 只要一個參數, 你寫成全域當然要
08/19 00:27, 10F

08/19 00:27, , 11F
說真的我只把他改成跟別的operator函式宣告一樣
08/19 00:27, 11F

08/19 00:27, , 12F
兩個, 注意一下 String:: 有沒有加上去
08/19 00:27, 12F

08/19 00:27, , 13F
loveme已經說了 operator==(const String & a)其實裡面
08/19 00:27, 13F

08/19 00:27, , 14F
還有在operator==前面加上String:: 這樣答案就出來了@@
08/19 00:27, 14F

08/19 00:27, , 15F
有改過後的程式碼嗎?
08/19 00:27, 15F

08/19 00:29, , 16F
operator==(xxx this,const String & a)那個xxx是class
08/19 00:29, 16F

08/19 00:29, , 17F
名稱 那個this指標會被隱藏
08/19 00:29, 17F

08/19 00:29, , 18F
改過的程式碼 http://tinyurl.com/2fwzhe4
08/19 00:29, 18F

08/19 00:30, , 19F
給字串符變數的那個版本的ctor記得加上const, 其實只
08/19 00:30, 19F

08/19 00:30, , 20F
不過==放外面寫兩個參數比較好吧 不然 "xxx"==某物件 這
08/19 00:30, 20F

08/19 00:30, , 21F
OK了 是我眼殘忘記加String = = 謝謝大家~
08/19 00:30, 21F

08/19 00:30, , 22F
樣應該會call不到
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
這樣寫可以是因為有些從C就繼承下來的使用慣例, 只要
08/19 00:32, 26F

08/19 00:32, , 27F
一改, 程式就會當, 另外像nowar100所說, 的確定義成
08/19 00:32, 27F

08/19 00:33, , 28F
non-member function 會比較好, 除了可以支援比較多的
08/19 00:33, 28F

08/19 00:34, , 29F
寫法, 還有就是函式耦合度的問題
08/19 00:34, 29F

08/19 00:36, , 30F
operator==(xxx *this,const String & a) 漏了指標...
08/19 00:36, 30F
文章代碼(AID): #1CR09akV (C_and_CPP)
文章代碼(AID): #1CR09akV (C_and_CPP)