When you use C # or Borland C Builder, you will meet this "property", you can control the way you write. Standard C does not have this concept, then can we use this feature in pure C ? of course can. Below is an implementation method that I have completed independently:
#include using namespace std;
Typedef enum {readonly, readwrite} rw_right;
Template Class Property {public: typedef _ty * (_CLASS :: * getter) (VOID); typedef void (cyst _ty "; public: proty (_class & _src / * target object * /, const getter _getter / * value function * /, const Setter _Setter / * Assignment function * /): m_src (_src), m_getter (_getter), m_setter {} Operator_ty & () / * Return Lvalue * / {Return * (M_SRC. * M_getter) (); / * You can directly assign the original data * /} operator const_ty & () const {return * (m_src. * M_getter (); }_ty & operator = (const_ty & _value) / * Return to a Lvalue * / {(m_src. * M_setter) (_Value); return * (m_src. * m_getter) ();} private: _class & m_src; / * Save the host class information * / getter m_getter; / * Host value * / setter m_setter; / * Host assignment function * / Property (); / * must initialize * / Property (Const Property &);};
Template class proty <_class, _ty, readonly / * read-only attribute test template * /> {public: typedef _ty * (_class :: * getter) (void); public: Property (_CLASS & _src, const getter _getter: m_src (_src), m_getter (_getter) {} operator const_ty () const / * can only return RVALUE * / {Return * (m_src. * m_getter) ();} private: _class & m_src; Getter m_getter; Property (); Property (constdition "; prot preoperator =; _ty & operator = (const _ty & _value); / * Prohibition of assignment * /};
Class myclass {public: int * get_count () {cout << "getting protection" << Endl; returnium & m_count;} void set_count (const INT & _COUNT) {cout << "setting property" << Endl; m_count = _count;} Public: myclass (): count (* this, & myclass :: get_count) {} Property count; private: int m_count;}; int main (int Argc, char * argv [in) ]) {MyClass Mc; mc.count = 2; int J = mc.count; cout << j << endl; return 0;}
The above test program compiles WARNINGWARNING C4355: "THIS": Used for the list of initial values of the base member
do not worry. VC 7.1 Write debugging, I have copyright, and there is an opinion, please send a short message.