2014-09-23 43 views
2

我重載運營商的數據結構,所以我有標準的函數聲明:運算符重載C++:只寫版本

T & operator[](int i); //used for regular objects 
const T & operator[](int i) const; // used for const objects 

所以我想要做的是有運營商[雙版本]用於常規對象:當使用operator []寫入而不是讀取時,會執行一些不同的操作。

我一直在閱讀,這是可能的,但我還沒有看到任何代碼。

我見過很多次這個問題,我看過答案''operator [] const'版本用於閱讀「 - >但這不是真的;它僅用於類的const實例化。

任何人都可以提供指導檢測寫事件觸發不同的行爲? 也許是複製構造函數中的技巧?

+5

AFAIK你可以在C做到這一點的唯一途徑++是返回一個代理值。 – milleniumbug 2014-09-23 22:00:36

+0

在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

+0

或者這個:[Vector,proxy class和C++中的點運算符](https://stackoverflow.com/questions/7182963/vector-proxy -class-and-dot-operator-in-c) – Deduplicator 2014-09-23 22:06:47

回答

0

正如在評論中提到的一樣,您需要返回一個代理,AKA智能引用,它是您想要的實際類型T的包裝,並且鏈接到調用[]運算符的原始對象。

當代理位於=的左側時,編譯器將調用其賦值運算符來鍵入T. 當代理位於=的右側時,編譯器將調用其演算操作符來鍵入T.

這裏是一個字符串例如,我想在斯科特Meyer的有效的C++ 描述主要的第一行應打印「寫入字符串」 第二行將打印

struct String 
{ 
    char text[10]; 
    struct CharReference; // forward declare char proxy class 
    CharReference String::operator[] (int index); 
    void print() { printf("%s",text); } 
}; 




struct String::CharReference 
{ CharReference(String * s, int i) ; 
    operator char(); 
    const char & String::CharReference::operator = (const char & rhs) ; 
    int index; 
    String *theString; 
}; 

const char & String::CharReference::operator = (const char & rhs) 
{ 
    printf("Writing to the string\n"); 
    theString->text[index] = rhs; 
    return rhs; 
} 

String::CharReference::operator char() 
{ 
    printf("Reading from the string\n"); 
    return theString->text[index]; 
} 

String::CharReference::CharReference(String * s, int i):theString(s), index(i) {} 

String::CharReference String::operator[] (int index) 
{ 
    return CharReference(this, index); 
} 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    String s; 
    s[0] = 'A'; 
    char c = s[0]; 
    return 0; 
} 
0
「從字符串讀」

持有對象的類無法獲取有關信息呃您對返回對象的訪問權限是讀取或寫入權限。

只有對象本身有一些「我在哪個上下文中使用」的概念,通過成員函數限定符。

  • REF-預選賽
  • 常量/ volatile限定符

您可以在代理類使用。

#include <vector> 
#include <type_traits> 
#include <iostream> 

template <class T, class U = T, bool Constant = std::is_const<T>::value> 
class myproxy 
{ 
protected: 
    U& m_val; 
    myproxy& operator=(myproxy const&) = delete; 
public: 
    myproxy(U & value) : m_val(value) { } 
    operator T &() 
    { 
    std::cout << "Reading." << std::endl; 
    return m_val; 
    } 
}; 

template <class T> 
struct myproxy < T, T, false > : public myproxy<T const, T> 
{ 
    typedef myproxy<T const, T> base_t; 
public: 
    myproxy(T & value) : base_t(value) { } 
    myproxy& operator= (T const &rhs) 
    { 
    std::cout << "Writing." << std::endl; 
    this->m_val = rhs; 
    return *this; 
    } 
}; 

template<class T> 
struct mycontainer 
{ 
    std::vector<T> my_v; 
    myproxy<T> operator[] (typename std::vector<T>::size_type const i) 
    { 
    return myproxy<T>(my_v[i]); 
    } 
    myproxy<T const> operator[] (typename std::vector<T>::size_type const i) const 
    { 
    return myproxy<T const>(my_v[i]); 
    } 
}; 

int main() 
{ 
    mycontainer<double> test; 
    mycontainer<double> const & test2(test); 
    test.my_v.push_back(1.0); 
    test.my_v.push_back(2.0); 
    // possible, handled by "operator=" of proxy 
    test[0] = 2.0; 
    // possible, handled by "operator T const&()" of proxy 
    double x = test2[0]; 
    // Possible, handled by "operator=" of proxy 
    test[0] = test2[1]; 
} 

打印

Writing 
Reading 
Reading 
Writing