A concise implementation of C + + class member properties __c++

Source: Internet
Author: User
Tags define get
A simple implementation of C + + class member properties

Generally speaking, for standard C + + there is no concept of member attribute, before we all use getxxx/setxxx to access or obtain data, it seems not to feel any inconvenience. But after we've used a language like C #, we always feel that C + + is a cliché. So we want to implement the "attributes" of the C + + language lack of elements. In fact, a lot of people on the network have done this part of the work, there are many ways to implement, one is to use a template, one is written in a specific language, such as VC (refers to the Microsoft implementation of C + +). But they are either very complex or difficult to remember the exact usage of, well, I always like the simple things, because too complicated stuff will make my mind machine. Talk less and see how it is achieved.
Before I can implement it, I need to explore why "attribute" is needed. 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, we can directly assign them to the value or read, but often the lack of a very important thing, that is, can not be assigned to verify the value, which is a big problem, such as we give the old a negative value, such as-50, the program runs without any errors, But it is true that the value of this member variable is logically incorrect. So we'll write Getold, Setold. OK now, this little problem has been solved, but the new problem has come. Our class users, they need to rearrange their code to look like the following, not the above.
CEmployee employee; Employee.   Setold (22); int old =employee. Getold ();
Your partner will surely curse you when writing code to write a garbage class. So you decided to change the situation. Luckily, you're a loyal user of MS, and you're very careful about MSDN, so you know you can write that.
Class CEmployee {private:int m_old: _declspec (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 is perfect for the goal of a property, but there is a small problem, such as my more stupid often need to find MSDN to know _declspec (get= getold,put=setold) int old; The meaning of this sentence, and I often forget its specific wording, such as put I often write it as set, it always reminds me of the good times to use C #, it can be written like this
public class CEmployee {private int m_old;   public int Old {get {m_old;}      set {if (value >0 && value <60) {m_old = value;      else {m_old = 20; }   }   }  }
So I thought I could take advantage of the powerful weapon macros in C + +, and we'll define a few 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 properties #define SETPROP (t,x) __declspec (property (put= __put# #X)) T X; Write only required #define GET (t,x) T __get# #X (void) #define SET (t,x) void __put# #X (T value)
Description: T represents the type of property such as Int,double,cstring, and X represents the property name. If you need a read-only property to use Getprop, write-only properties can use Setporp, and then use a get or set, of course, if you use prop, but only a get or set, there is no fault, but at compile time will tell you that there is no __getxxx or _ The _putxxx method. Then we can write our class like this.
Class CEmployee {private:int m_old; public:prop (int, old) get (int,old) {return m_old;   SET (Int,old) {if (value >0) && (value <60)//Here's value you can take it as C # as the keyword {m_old = value;   else {m_old = 20; }  } };
Well, the work we have to do is done. Of course, there are many problems with this approach, such as the use of C # commonly used indexed properties, static properties, and so on. But after all, we are C + + programmers, hehe. Finally, this method is only useful under VC.


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.