This article is submitted by Emad Barsoum. Development test environment: Visual C 7.0, Windows XP SP1, Windows 2000 SP3
Abstract This article tried to use any extended techniques in the C # (or other languages) in C . Most libraries that implement attributes in C use extension technology, such as Managed C or C Builder, or they use the set and get methods such as usual functions, but that is not attribute.
Detail We first look at what is attribute. A property is behaving as a field or a member variable, but it imparts the operation variables via the READ and WRITE or GET and SET methods.
For example, if there is class A and its attribute count, I can write the following code: a foo; cout << foo.count; actually calling its GET function Returns the current variable value. You can set the properties as read-only (you can read it but you can't modify it), write only or read, which is the maximum benefit of using attributes without direct use of variables. Ok, let's start to implement it:
We need to do the following things: int i = foo.count; // - Call the get function to get the value foo.count = i; // - call the set function setting value
Therefore, it is obvious that we need to overload the '=' operator to set the value of the variable, but also to overload the return value of the property (below we will see below).
We will implement a class called Property, which is like a property, declares that the Template
#define read_only 1 # Define Write_only 2 # define read_write 3
Template
// - Point m_cobject to the ContaId setContainer (ContaInd = COBject;} // - settings to change the property value - Void Setter (contaId). : * pset ") {if ((npropType == Write_only) || (NPROPTYPE == Read_WRITE) SET = Pset; Else Set = NULL;} // - Set Get members that can retrieve attribute values Function --void getter ()) {if ((NPROPTYPE == = = = = = = = = = = = = = = = = read_write) get = pget; else get = null;} // The load of the '=' operator allows it to set the attribute value with the SET member --ValueType Operator = (const valueT! = Null) {assert (m_cobject! = Null); assert (set! = Null); (M_CObject -> * SET (Value); Return Value;} // - Make the attribute class to internal types becomes possible --Operator valuePE () {assert (m_cobject! = null); assert (gEt! = null); return (m_cobject- > * Get) ();} private; // - Point to class modules containing properties - VoID (VALUETYPE VALUE); // - Function pointer to the SET member function - ValuetyPE (); // - Pointing the function pointer to the GET member function -}; now let us see these code for a while:
In the following code, only the Container pointer points to an instance of a valid containing attribute. Void setContainer (Container * cobject) {m_cObject = cobject;} The following code, setting the pointer points to the SET and GET member functions in the class containing the properties, its set and get member function, the only restriction is the SET member function must There is no return value without returning values, but the GET member function does not have a parameter, but it is necessary to return the value of the valueetype.
// - Set the SET member function that can change attribute value --void setter (ValueType Value) {if ((nproptype == write_only) || (npropType == Read_Write)) Set = pset; else set = null;} // - Set the GET member function that can retrieve attribute values --void getter ((NPROPTYPE == Read_only) | | (npropType == read_write)) Get = Pget; Else get = null;} In the following code, the first part is the overload of the '=' operator, which calls the SET function in the class containing the attribute settings its properties. Value. The second part is to function as the entire attribute class like the ValueType type, so it returns the return value of the GET function in the class containing the attribute. // - Overload the '=' operator to set the attribute value with the SET member - ValueType Operator = (const valueT! = Null) {assert (m_cobject! = Null); Assert (set! = Null); m_cobject -> * set); return value;} // - Make the attribute class to internal types becomes possible --Operator valueTYPE () {assert (m_cobject! = null); assert (get! = null) Return (M_CObject -> * GET) ();
Now let's take a look at how to use it: as shown below, define a simple property called Count in the Proptest class. The actual value of Count will be saved or retrieved in the Proptest's private member variable "m_ncount", through the GET and SET methods of PROPTEST. GET and SET methods can use any variable names, only their address can be passed to the Property class, such as code, code line "Property
To make the count property successfully, you must initialize it in the constructor of the PropTest.
class PropTest {public: PropTest () {Count.setContainer (this); Count.setter (& PropTest :: setCount); Count.getter (& PropTest :: getCount);} int getCount () {return m_nCount;} void setCount (int Ncount) {m_ncount = ncount;} Property
PRIVATE: INT M_NCOUNT;
As shown below, you can use the count attribute like using normal variables.
INT i = 5, J; PropTest Test; Test.count = i; // - Call SET Function - J = Test.count; // Call Get Function - To use read-only properties, you can create The following Property instance: Property