C++ (Qt)struct CArray { ... int Get( size_t index ) const; void Set( size_t index. int val );};
C++ (Qt)CArray arr;..for (int i = 0; i < limit; ++i) if (arr[i] < 0) arr[i] = 0;
C++ (Qt)void CArray::Set( size_t index, int val ){ mReal[index] = double(val); // mReal, mImage - контейнеры double mImage[index] = 0.0;}
class CArray {...............int operator[] (size_t idx) const;int& operator[] (size_t idx);...............};
C++ (Qt)#include <QCoreApplication>#include <QDebug> struct Array{ int get(size_t /*index*/) const { return 0; } void set(size_t /*index*/, int /*val*/) {} int& operator[](int index); QList<int> m_list;}; int& Array::operator[](int index){ return m_list[index];} int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); Array array; array.m_list << 1 << 2 << 3 << 4 << 5 << 6 << 7; qDebug() << array[2]; array[2] = 100500; qDebug() << array[2]; return a.exec();}
C++ (Qt)class adapter{public: adapter(int& tmp, double& re, double& im ) : m_tmp(tmp), m_re(re), m_im(im) {} int& value() { return m_tmp; } ~adapter() { m_re = m_tmp; m_im = 0.0; }private: int& m_tmp; double& m_re; double& m_im;}; struct CArray{ CArray() {} int& operator[](size_t index) { double& re = mReal[index]; double& im = mReal[index]; adapter adap(m_tmp, re, im); return adap.value(); } enum {SIZE = 100}; double mReal[SIZE]; double mImag[SIZE]; int m_tmp;}; int main(){ CArray arr; arr[1] = 2; std::cout << arr[1] << std::endl; return 0;}
#include <iostream>class CArray {public: int get(int index) const { int res = (index & 1) ? 1 : -1; std::cout << "get(" << index << ") = " << res << std::endl; return res; } void set(int index, int value) { std::cout << "set(" << index << ", " << value << ")" << std::endl; } struct Helper { CArray& a; int i; Helper(CArray& _a, int _i) : a(_a), i(_i) {} Helper& operator=(int value) { a.set(i, value); return *this; } operator int() { return a.get(i); } }; Helper operator[](int index) { return Helper(*this, index); }};int main(){ CArray arr; for (int i = 0; i < 10; ++i) { if (arr[i] < 0) arr[i] = 0; } return 0;}