C++ (Qt)class A { A( int & data ) : mData(data) {} int & mData; int mOther;};
class ReadOnlySegmentTreeModel : public QAbstractItemModel{public: explicit ReadOnlySegmentTreeModel(const Segment& rootSegment, QObject* parent = 0); ............private: const Segment* rootSegment_; // acts as reassigned reference};
explicit ReadOnlySegmentTreeModel(const Segment& rootSegment, QObject* parent = 0);
C++ (Qt)template <class T>class rew{public: void set(T & a) { ptr = std::make_shared<std::reference_wrapper<T>>(a); } T& operator()() { return ptr->get(); } operator bool() const { return bool(ptr); } private: std::shared_ptr<std::reference_wrapper<T>> ptr;}; class A{public: void setData(int & data) { mData.set(data); } void someMethod() { if (!mData) return; mData() += 123; std::cout << mData() << std::endl; } private: rew<int> mData; int mOther;}; int main(){ int a = 2; A test; test.setData(a); test.someMethod(); std::cout << a << std::endl; return 0;}
C++ (Qt) void set(T & a) { ptr = std::make_shared<std::reference_wrapper<T>>(a); }
C++ (Qt)class A { ... void SetRef( int & ref ) { (&mDummy)[1] = &ref; } void * mDummy; int & mData;};