作者supercygnus (......)
看板Programming
標題[問題] 設定運算子"="多載的改寫
時間Wed Jun 22 16:39:56 2011
#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
→ firejox:友誼 ->google friend c++ 123.240.129.15 06/22 20:14
→ firejox:一般 把object丟到public 123.240.129.15 06/22 20:15
→ supercygnus:定義我都知道,但是就是寫不出來= = 118.168.161.38 06/23 00:54
→ james732:就我所知opeartor=只能寫member function 140.117.171.46 06/23 14:05
→ firejox:你的operator型態... 123.240.129.15 06/23 19:25
推 tiwei:void 改成CWin 24.62.61.145 06/28 12:35