我重載運營商的數據結構,所以我有標準的函數聲明:運算符重載C++:只寫版本
T & operator[](int i); //used for regular objects
const T & operator[](int i) const; // used for const objects
所以我想要做的是有運營商[雙版本]用於常規對象:當使用operator []寫入而不是讀取時,會執行一些不同的操作。
我一直在閱讀,這是可能的,但我還沒有看到任何代碼。
我見過很多次這個問題,我看過答案''operator [] const'版本用於閱讀「 - >但這不是真的;它僅用於類的const實例化。
任何人都可以提供指導檢測寫事件觸發不同的行爲? 也許是複製構造函數中的技巧?
AFAIK你可以在C做到這一點的唯一途徑++是返回一個代理值。 – milleniumbug 2014-09-23 22:00:36
在C++中重載操作符\ [\],但爲了防止\ [i \] = one \ _special \ _specific \ _value](http://stackoverflow.com/questions/20105097/overloading-operator-in -c-but-to-prevent-ai-one-special-specific-value) – Deduplicator 2014-09-23 22:05:58
或者這個:[Vector,proxy class和C++中的點運算符](https://stackoverflow.com/questions/7182963/vector-proxy -class-and-dot-operator-in-c) – Deduplicator 2014-09-23 22:06:47