作者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
1F:→ firejox:友谊 ->google friend c++ 123.240.129.15 06/22 20:14
2F:→ firejox:一般 把object丢到public 123.240.129.15 06/22 20:15
3F:→ supercygnus:定义我都知道,但是就是写不出来= = 118.168.161.38 06/23 00:54
4F:→ james732:就我所知opeartor=只能写member function 140.117.171.46 06/23 14:05
5F:→ firejox:你的operator型态... 123.240.129.15 06/23 19:25
6F:推 tiwei:void 改成CWin 24.62.61.145 06/28 12:35