Visual c ++'s "virtual attributes" Function

Source: Internet
Author: User

"Virtual attributes" function of Microsoft Visual C ++

 

Note:

This article briefly introduces how to use the _ declspec keyword in Microsoft Visual C ++ to implement features that are not included in C ++. For more information about the _ declspec keyword, refer to msdn.

The _ declspec keyword is not part of the standard C ++. Therefore, this "attribute" method is only applicable to Visual C ++, to learn how to simulate "attributes" in Standard C ++, refer:

Http://www.csdn.net/develop/read_article.asp? Id = 18361

 

Body:

Many legacy traditional C ++ code often contain member variables modified with the public or protected keyword. You can directly access them, instead of using a simple get/set method. For example, the following structure definition is like this:

Typedef struct tagmystruct
{
Long m_lvalue1;
... // Rest of the structure definition.
} Smystruct;

In the client program using this struct, we can see that a large number of classes are scattered, as listed below:

Smystruct mystruct;
Long ltempvalue;

Mystruct. m_lvalue1 = 100; // or any other value that is to be assigned to it.
...
Ltempvalue = mystruct. m_lvalue1;

In this case, once the code needs to be applied in a multi-threaded environment, you will encounter a problem. Because the get/set method does not exist, you cannot simply add a critical section (or mutex) in the definition of smystruct to protect all public member variables, including m_lvalue1.

If you are using the Microsoft Visual C ++ compiler, you can easily find a solution to this problem.

You only need to rewrite your struct as follows:

Typedef struct tagmystruct
{
_ Declspec (Property (get = getvalue1, put = putvalue1 ))
Long m_lvalue1;
... // Rest of the structure definition.
Long getvalue1 ()
{
// Lock critical section
Return m_linternalvalue1;
// Unlock critical section.
}
Void putvalue1 (long lvalue)
{
// Lock critical section
M_linternalvalue1 = lvalue;
// Unlock critical section
}
PRIVATE:
Long m_linternalvalue1;
// Define critical section member variable.
} Smystruct;

This is all you need to do!

After that, for the following code:

Mystruct. m_lvalue1 = 100

The compiler automatically converts:

Mystruct. putvalue (100)

For the following code:

Ltempvalue = mystruct. m_lvalue1

The compiler automatically converts:

Ltempvalue = mystruct. getvaluel ()

This feature provides many useful functions. You can even use it to add the reference counting function to your original struct or class!

 

Added:

VC also provides corresponding support for classes such as arrays, as shown in the following example:

# Include <iostream>
Using namespace STD;

Class mystruct
{
Public:
_ Declspec (Property (get = getvalue1, put = putvalue1 ))
Int T [] []; // use a two-dimensional array for demonstration
Int getvalue1 (int x, int y) // X and Y correspond to the subscript of the first dimension and the second dimension respectively.
{
Return m_linternalvalue1 [x] [Y];
}
Void putvalue1 (int x, int y, int lvalue) // X and Y correspond to the subscript of the first dimension and the second dimension respectively. lvalue is the value to be assigned.
{
M_linternalvalue1 [x] [Y] = lvalue;
}
PRIVATE:
Int m_linternalvalue1 [3] [3];

};

Int main ()
{
Mystruct MS;
For (INT I = 0; I <3; I ++)
For (Int J = 0; j <3; j ++)
Ms. t [I] [J] = I * J;
Return 0;
}

In vc6 and vc7, the processing of multi-dimensional arrays is slightly different.

_ Declspec (Property (get = getvalue1, put = putvalue1 ))
Int T [] [];

In vc6, you can simply write int T []; To support two-dimensional arrays, and in vc7, you must write int T.

 

Source selected from: http://www.codeproject.com/cpp/virtual_property.asp

 

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.