Then talk about using C + + to implement property, do not understand what those committees are eating.

Source: Internet
Author: User

See some articles, do a VC, CB, GCC General Property Declaration macro function, by CB and Devcpp test pass, release version of efficiency without loss. I don't know what the C + + committee is eating, doing that standard library all day, but refusing to expand the basic functionality. Object-oriented class three operations: Methods, properties, events, where properties will not provide the editor support standards, CB and VC many years ago to support, not C + + can not do, just in the programming process, those get () and set () The Operation function is not beautiful, writing also laborious, the whole look up, There are too many brackets ()!

The code is as follows:

Property

#if defined (__borlandc__) | | defined (_MSC_VER)
#define PROPERTY_GET_SET (class, type, name, getter, setter) \
__declspec (property (Get=getter, Put=setter)) type name;
#define PROPERTY_GET (class, type, name, getter) \
__declspec (property (get=getter)) type name;
#define Property_begin
#define Property_end
#else//defined (__borlandc__) | | defined (_MSC_VER)
//The property of the simulation is occupied by Space
#define Property_get_set (class_t, type_t, name, getter, setter) \
class property_# #name \
{\
Public:\
Friend class Class_t;\
Inline size_t offset () {return offsetof (class_t, name);} \
inline class_t* parent () {return reinterpret_cast<class_t*> (size_t (This)-offset ());} \
template<typename t> inline operator T () \
{\
return T (parent ()->getter ()); \
}\
inline property_# #name & operator= (const type_t& n) \
{\
parent ()->setter (n);
return *this;\
}\
}name;


#define Property_get (class_t, type_t, name, getter) \
class property_# #name \
{\
Public:\
Friend class Class_t;\
Inline size_t offset () {return offsetof (class_t, name);} \
inline class_t* parent () {return reinterpret_cast<class_t*> (size_t (This)-offset ());} \
template<typename t>inline operator T () \
{\
return T (parent ()->getter ()); \
}\
private:\
property_# #name & operator= (const type_t& n); \
}name;


//Put property in a union, can effectively reduce the occupation of class space
#define Property_begin union{
#define Property_end};
#endif//defined (__borlandc__) | | defined (_MSC_VER)

Then talk about using C + + to implement property, do not understand what those committees are eating.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.