What can "_ declspec" in VC do? (2)-add attributes to the class

Source: Internet
Author: User
Tags gety

Attribute is an indispensable element in Object-Oriented Programming. A generalized attribute is used to describe the state of an object. The attribute mentioned in this article is narrow. It refers to the ability to use the "=" operator to perform get or set operations on a data of the class and to control the get and Set permissions.
First look at the Code:

 #include <IOSTREAM>
#include <map>
#include <string>
#include <CONIO.H>
using namespace std;

class propertytest
{
    int m_xvalue;
    int m_yvalues[100];
    map<string,string> m_zvalues;
public:
    __declspec(property(get=GetX, put=PutX)) int x;
    __declspec(property(get=GetY, put=PutY)) int y[];
    __declspec(property(get=GetZ, put=PutZ)) int z[];

    int GetX()
    {
        return m_xvalue;
    };
    void PutX(int x)
    {
        m_xvalue = x;
    };
    
    int GetY(int n)
    {
        return m_yvalues[n];
    };

    void PutY(int n,int y)
    {
        m_yvalues[n] = y;
    };

    string GetZ(string key)
    {
        return m_zvalues[key];
    };

    void PutZ(string key,string z)
    {
        m_zvalues[key] = z;
    };

};

int main(int argc, char* argv[])
{
    propertytest test;
    test.x = 3;
    test.y[3] = 4;
    test.z["aaa"] = "aaa";
    std::cout << test.x <<std::endl;
    std::cout << test.y[3] <<std::endl;
    std::cout << test.z["aaa"] <<std::endl;

    getch();
    return 0;
}

Related Articles:

What Can _ declspec in VC do?-Preface

Chapter 1 What Can "_ declspec" in VC do? (1)-define interfaces

Chapter 2 What Can "_ declspec" in VC do? (2)-add attributes to the class

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.