[問題] &operator vs operator
class point
{
private:
bool changed;
double data[3];
public:
double &operator [](int i)
{
changed = true;
return data[i];
}
double operator [](int i) const
{
return data[i];
}
}
重載了兩個operator [],
主要是希望程式可以判別出
point[0] = 5;//changed = true
double x = point[0];
的不同,
但程式現在即使是
x = point[0];
他也是呼叫
double &operator [](int i)
請問 class 可以做出此功能嗎?
判別資料是否發生改變
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 110.27.135.136
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1448783280.A.B06.html
→
11/29 15:55, , 1F
11/29 15:55, 1F
→
11/29 15:56, , 2F
11/29 15:56, 2F
推
11/29 17:40, , 3F
11/29 17:40, 3F
→
11/29 17:41, , 4F
11/29 17:41, 4F
→
11/30 22:16, , 5F
11/30 22:16, 5F
→
11/30 22:16, , 6F
11/30 22:16, 6F
→
12/01 12:07, , 7F
12/01 12:07, 7F
→
12/01 12:08, , 8F
12/01 12:08, 8F
class point{
proxy & oprator[] (int n){
return proxy(data[n],changed)
}
}
class proxy class{
private:
bool *changed;
double *data;
public:
proxy(double &d,bool &b){
data = &d;
changed = &b;
}
proxy &oprator = (double d){
*changed = true;
*data = double;
return this;
}
double operator (double){
return *data;
}
}
除
operator =, operator double
之外,
還必須自己重寫
operator +-×÷
+=,-=,…
等運算吧?
(抱歉,我還沒試這邊)
這些介面操作方法,
會讓class變的需更多時間
在建構/解構上嗎
※ 編輯: noodleT (110.26.71.227), 12/01/2015 21:57:45
※ 編輯: noodleT (110.26.71.227), 12/01/2015 22:02:04
推
12/01 22:33, , 9F
12/01 22:33, 9F
→
12/01 22:34, , 10F
12/01 22:34, 10F
→
12/01 22:38, , 11F
12/01 22:38, 11F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章