[問題] 設定運算子"="多載的改寫

看板Programming作者 (......)時間14年前 (2011/06/22 16:39), 編輯推噓1(105)
留言6則, 4人參與, 最新討論串1/1
#include <iostream> #include <cstdlib> using namespace std; class CWin { private: char id, *title; public: CWin(char i='D', char *text="Default window"):id(i) { title=new char[50]; strcpy(title,text); } void set_data(char i, char *text) { id=i; strcpy(title,text); } void operator=(const CWin &win) { id=win.id; strcpy(this->title,win.title); } void show(void) { cout << "Window " << id << ": " << title << endl; } ~CWin(){ delete [] title; } CWin(const CWin &win) { id=win.id; strcpy(title,win.title); } }; int main(void) { CWin win1('A',"Main window"); CWin win2; win1.show(); win2.show(); win1=win2; cout << endl << "after win1=win2" << endl; win1.show(); win2.show(); win1.set_data('B',"Hello window"); cout << endl << "更改win1之後" << endl; win1.show(); win2.show(); system("pause"); return 0; } (1)將operator=()函數改成以友誼函數來撰寫 (2)將operator=()函數改成一般的函數來撰寫 請問這兩個要怎麼寫呢@@ 寫了好久還是想不出來= = compile過不了= = -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.136.211.19

06/22 20:14, , 1F
友誼 ->google friend c++
06/22 20:14, 1F

06/22 20:15, , 2F
一般 把object丟到public
06/22 20:15, 2F

06/23 00:54, , 3F
定義我都知道,但是就是寫不出來= =
06/23 00:54, 3F

06/23 14:05, , 4F
就我所知opeartor=只能寫member function
06/23 14:05, 4F

06/23 19:25, , 5F
你的operator型態...
06/23 19:25, 5F

06/28 12:35, , 6F
void 改成CWin
06/28 12:35, 6F
文章代碼(AID): #1E0QfVJy (Programming)
文章代碼(AID): #1E0QfVJy (Programming)