A concise implementation of C ++ class member attributes

Source: Internet
Author: User
Tags define get
Generally, for standard C ++, the concept of member attributes does not exist. In the past, we used GetXXX/SetXXX to access or retrieve data, and we did not feel any inconvenience. However, after we use a language like C #, we always feel that the C ++ method is too old. So we want to implement the lack of C ++ language "attribute. In fact, many people have already done this part of work on the network, and there are many implementation methods. One is to use templates and the other is to write based on specific languages, for example, VC (C ++ implemented by Microsoft ). However, they are either complicated or difficult to remember their accurate usage. Well, I always like simple things, because too complicated things will make my mind a machine. Let's talk about how to implement it.

Before implementation, I must first discuss why "attributes" are required. For example, the following employee class:

Class CEmployee

{

Public:

Int Old; // age

};

CEmployee employee;

Employee. Old = 22;

Int old = employee. Old;

It has a member variable, which can be assigned or read directly, but it is often missing a very important thing, that is, it cannot be checked for the assigned value. This is a big problem, for example, if we give Old a negative value, such as-50, it indicates that there will be no errors during the program running, but it is true that the value of this member variable is logically incorrect. So we will write GetOld and SetOld. Now OK, this small problem is solved, but a new problem is coming. Our class users need to write their code again as follows, instead of the above.

CEmployee employee;

Employee. SetOld (22 );

Int old = employee. GetOld ();

Your partner will curse you when writing code to write a junk class. So you decide to change this situation. Fortunately, you are a loyal user of MS, and you are very careful with MSDN, so you know you can write it like this.

Class CEmployee

{

Private:

Int m_old;

Public:

_ Declspec (property (get = GetOld, put = SetOld) int Old;

Int GetOld (void)

{

Return m_old;

}

Void SetOld (int value)

{

If (value> 0) & (value <60 ))

{

M_old = value;

}

Else

{

M_old = 20;

}

}

};

Very Good, the above class perfectly achieves the goal of a property, but there is still a small problem, such as me stupid often need to find MSDN will know

_ Declspec (property (get = GetOld, put = SetOld) int Old;
The meaning of this sentence, and I often forget the specific writing method, for example, put, I often write it as set, which always reminds me of the good time to use C, it can be written like this.

Public class CEmployee

{

Private int m_old;

Public int Old

{

Get

{Return m_old ;}

Set

{

If (value> 0 & value <60)

{

M_old = value;

}

Else

{

M_old = 20;

}

}

}

}

So I think we can use the powerful weapon Macros in C/C ++ to define several macros.

# Define PROP (T, X) _ declspec (property (get = _ get # X, put = _ put # X) t x;

# Define GETPROP (T, X) _ declspec (property (get = _ get # X) t x; // read-only attribute

# Define SETPROP (T, X) _ declspec (property (put = _ put # X) t x; // only write is required

# Define GET (T, X) T _ get # X (void)

# Define SET (T, X) void _ put # X (T value)

Description: T indicates the property type, such as int, double, CString, and X indicates the property name. If you need a read-only property, you can use GETPROP. If you only want to write the property, you can use SETPORP and then use a GET or SET. Of course, if you use PROP, you only use one GET or SET, there is no error, but the compilation will tell you that there is no _ getXXX or _ putXXX method. Then we can write our class in this way.

Class CEmployee

{

Private:

Int m_old;

Public:

PROP (int, Old)

GET (int, Old)

{

Return m_old;

}

SET (int, Old)

{

If (value> 0) & (value <60) // The value here can be used as a keyword like C #.

{

M_old = value;

}

Else

{

M_old = 20;

}

}

};

Now, we have finished our work. Of course, this method still has many problems, such as the inability to use index attributes and static attributes commonly used in C. But after all, are we C ++ programmers! Finally, this method is only useful in VC.

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.