Attributes in C + +

Source: Internet
Author: User
Tags strcmp

Attributes in C + +


C # has properties, but C + + is not, in fact, the attribute is also very important for C + +, please look down.


What is an attribute

Properties are like variables that can store data, but when reading and writing data from them, an event is raised, in other words, an attribute is an interacting variable that updates itself and produces a different value when reading and writing to itself.
Using a language that contains attributes like C # is very easy to write a class, but it seems unlikely for C + + because the compiler for C + + does not support the form of C # 's properties. Because of this, this article is about how to write a C + + class with attributes like C #.


Why attributes are important

If you need to write an object that represents a person, this object may contain the following data: Full name, age, date of birth, gender. If you write with C + +, the code is as follows:


Class Person {
Public
Person () {}
Virtual ~person () {}

Private://data member

Char m_fname[20];
Char m_lname[20];

UINT M_yearofbirth;
BOOL M_bgender;
};


Note: In most cases, it is not possible to define a data member to be public for direct use, because data members should be maintained by the business logic implemented within the object.

If you need to set or read M_bgender values, you may want to implement the following methods:


Class Person {
Public
Person () {}
Virtual ~person () {}

void Setgender (bool bgender) {m_bgender = Bgender;}
BOOL Getgender () {return m_bgender;}

Private://data member

Char m_fname[20];
Char m_lname[20];

UINT M_yearofbirth;
BOOL M_bgender;
};


The disadvantage of this approach is that the name and use must be known beforehand, using attributes is much simpler, all you need to know is a property name, and a single property can support different data types, that is, in the example above, you can let gender accept a string or Boolean type, as follows:


Person.gender = "Male";

Or:

Person.gender = true;


Property declaration

Now, to see how to write a property, start with the gender property:


Class Person {
Public
Person () {}
Virtual ~person () {}

Begin_property (Char*,gender)
__get (Char*,gender)
_set (char*);

_get (BOOL);
_set (BOOL);

__release (Gender)
End_property (Gender)

Private://data member

Char m_fname[20];
Char m_lname[20];

UINT M_yearofbirth;
BOOL M_bgender;
};


The code uses the Begin_property macro to begin defining a property that accepts two parameters: the property data type and the property name. Because the gender property is a string property, it should be char *. You also need to declare the event get () and set () to raise the event, as follows:


This can cause the _set (bool) event
Person.gender = true;
This can cause the _get (bool) event
BOOL gender = Person.gender;
This can cause _set (char*) events
Person.gender = "Male";
This can cause _get (char*) events
printf ("Gender:%s/n", (char*) person.gender);


_get and _set are two macros that accept a parameter, which represents the data type acceptable to the attribute, and you can see that the data type of the _get and _set events is independent of the data type of the property, in other words, although the data type of the Gender property is char *, But it can also accept Boolean types.
The last two macros are: _release, which frees the memory it allocates, End_property, its End property declaration, and all two macros accept the property name as a parameter.


Implementation of attributes

After declaring the property, you need to implement set () and get (), see the following code:


......
Begin_property (Char*,gender)
__get (Char*,gender)
_set (char*)
{
Some code
return Gender;
}

_get (BOOL)
{
Some code
return ivalue;
}
_set (BOOL)
{
Some code
return ivalue;
}

__release (Gender)
End_property (Gender)
......


You can implement it here, and before you tell how to use these macros, explain the two-point question: What the __get macro is and why set () returns a value. In __get (char *), only a pointer is returned: "Return Gender", which is the default Getter property because "Gender" is a char * property and should be used in this way: "__get (char *,gender)."
To see the second question, why set () returns a value like get (). In simple terms, in C + +, set () can play the role of Get (), see the following code:


BOOL Bgender = Person.gender = true;


Now you can use Imp_set and imp_get two macros to implement the Gender property, and two macros accept three parameters: Data type, class name, and property name, as follows:


Imp_set (Char*,person,gender)
{
Property_prologue (Person,gender)

if (! Gender) Gender = new Char[7];

if (strlen (ivalue) <6)
{
int result;
if ((RESULT=STRCMP (Ivalue, "Male")) ==0)
Pthis->m_bgender = true;
Else
{
if ((RESULT=STRCMP (Ivalue, "Female")) ==0)
Pthis->m_bgender = false;
}

if (result==0) strcpy (Gender,ivalue);
}
return Gender;
}

Imp_set (Bool,person,gender)
{
Property_prologue (Person,gender)

if (! Gender) Gender = new Char[7];

if (Pthis->m_bgender = ivalue)
strcpy (Gender, "Male");
Else
strcpy (Gender, "Female");

return (bool) Ivalue;
}

Imp_get (Bool,person,gender)
{
Property_prologue (Person,gender)
Return pthis->m_bgender;
}


Because set () and get () cannot directly access class members, to use the Property_prologue macro, it defines a pointer to the property class: "PThis" as shown above.
Also, at the end of the class, you need to free up memory and all allocated resources, which is done by the __release macro. __release is the default release macro, and its code is as follows:


if (Gender)
{
Delete Gender;
Gender = NULL;
}


Alternatively, you can use _release and imp_release to implement the release event, as follows:


Imp_release (Person,gender)
{
All allocated resources are freed here
}


Sample Project Download: Properties_demo.zip

 

Related Article

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.