An attempt to implement the property function in C # In C ++: Macro (2)

Source: Internet
Author: User

The first version has some problems, so we need to improve it on this basis. Looking back at the property practices in C #, the private members of the class are actually accessed. For example, the Length attribute is generally a private member named _ length, it can be said that the property is bound to the member. The initial idea of this binding can be implemented using pointers.
The custom get/set function allows you to rewrite the = Operator and the custom conversion operator. But there is a big headache. An important purpose of the custom get/set function is to set the boundary condition or check. If this check does not depend on other values of the object (accurately speaking, non-static variables), the problem is not big, but it is quite common to depend on other values of the object, you need to obtain the Object Pointer using this attribute. This problem cannot be solved perfectly.
My solution is to use a base class to store pointers to objects using properties, but this involves the situation where code needs to be inserted in multiple places. If you have a better method, please feel free to contact me.
The macro definition of version 2 is as follows:

# Ifdef use_property
# Define declare_property (classname) typedef classname hostclass;
Class propertybase ...{
Public: inline static void setppowner (hostclass * thispoint)... {ppowner = thispoint ;}
Protected:
Static hostclass * ppowner;
};

# Define basicproperty (type, name, refobj) class type ## property _ # Name: propertybase {
Public:
Inline type operator = (type value)... {ppowner-> refobj = value; return value ;}
Inline operator type ()... {return ppowner-> refobj ;}
};
Type ## property _ # name;
// End of basicproperty define.

# Define complexproperty (type, name) class type # property _ # Name: propertybase {
Public:
Type Operator = (type AC );
Operator type ();
};
Type ## property _ # name;
// End of complexproperty define.
// Complexproperty shocould use Marco setor and getor to define its own get/set Behavior

# Define implement_property (classname) typedef classname hostclass;
Hostclass * hostclass: propertybase: ppowner = NULL;
# Define propertybase_init propertybase: setppowner (this );
# Define setor (type, name) type hostclass: Type ## property _ # Name: Operator = (type value)
# Define getor (type, name) hostclass: Type ## property _ # Name: operator type ()

# Define property (type, name, refobj) basicproperty (type, name, refobj)
# Define cproperty (type, name) complexproperty (type, name)

# Else
# Define declare_property (classname)
# Define implement_property (classname)
# Define basicproperty (type, name, refobj)
# Define complexproperty (type, name)
# Define propertybase_init
# Endif

Define use_property first. Insert a declare_property (classname) line in the beginning of the class definition, insert a line of implement_property (classname) before the class implementation, and insert a line of propertybase_init in all constructors of the class. (You can see that I am imitating some macros of MFC)
Then, the macro basicproperty (type, name, refobj) binds a member of the type in the class named refobj to the property name. This macro is similar to the first version, that is, the default set/get function. The difference is that with binding, you can directly access refobj in the class to change the attribute. This refobj needs to be manually defined. Of course, it should be a private or protected member.
Macro complexproperty (type, name) is a macro that solves the custom set/get function. The implementation method is very simple, that is, as mentioned above, rewrite = operators and custom conversion operators. After using this macro, you need to add the corresponding macro setor (type, name) and getor (type, name) to the class implementation file, and then write the corresponding function body. (Note that curly braces should be added) use the ppowner pointer (propertyowner, attribute owner) to use objects in the function ). Since the function body needs to be customized, the macro does not need to use the refobj parameter, and the corresponding class member can be used directly in the function body. (The names setor and getor are mainly implemented by funtor .... -_-!)

Note: Release the instance program using the method later.

Review: The handwritten setor and getor function bodies are designed to achieve the goal, so they do not increase the complexity. The key is to use the ppowner pointer, and its initialization makes it necessary to add code in several places. In particular, it is cumbersome to add the propertybase_init macro to all constructors of the class.
Currently, the read-only write function is not added, but it is not complicated. You only need to change the declaration of the = Operator and the custom conversion operator in the basicproperty macro definition and complexproperty macro definition to the macro declaration. By default, this macro does not change the current statement. To implement this macro, for example, read-only, declare the = Operator function as private. This is for everyone to complete. (We can also refer to the MFC practice here .)

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.