[問題] 設定運算子"="多載的改寫
#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
06/22 20:14, 1F
→
06/22 20:15, , 2F
06/22 20:15, 2F
→
06/23 00:54, , 3F
06/23 00:54, 3F
→
06/23 14:05, , 4F
06/23 14:05, 4F
→
06/23 19:25, , 5F
06/23 19:25, 5F
推
06/28 12:35, , 6F
06/28 12:35, 6F
Programming 近期熱門文章
PTT數位生活區 即時熱門文章
7
20