Using properties in C ++)

Source: Internet
Author: User

In C ++, attributes like C # can also be used. In some specific environments, we can use this method, although it is more efficient than direct access. However, this efficiency is negligible .. The code is roughly as follows:

 

# Include <iostream> <br/> using namespace STD; </P> <p> class test <br/>{< br/> public: <br/> int get (void) <br/>{< br/> return m_nlevel; <br/>}</P> <p> void set (INT value) <br/>{< br/> m_nlevel = value; <br/>}</P> <p> _ declspec (Property (get = Get, put = set )) int level; </P> <p> PRIVATE: <br/> int m_nlevel; <br/>}; </P> <p> int main () <br/>{< br/> test ts; <br/> TS. level = 100; <br/> cout <ts. level <Endl; <br/> system ("pause"); <br/> return 0; <br/>}

 

We use _ declspec (Property (get = ???, Put = ???) )???; To define the get and set methods of a member.

When we call this Member, we will automatically call the set or get method, and then pass our private member to our public property member.

We can also see the entire call process of the above Code according to the disassembly:

 

Main:
Push EBP
MoV EBP, ESP
Sub ESP, 0cch
Push EBX
Push ESI
Push EDI
Lea EDI, [ebp-0CCh]
MoV ECx, 33 H
MoV eax, 0 cccccccch
Rep STOs dword ptr [EDI]
Push 64 h
Lea ECx, [ts]
Call test: Set (41c433h) // set 100
Push offset STD: Endl (41c54bh)
Lea ECx, [ts]
Call test: Get (41c2a3h) // get m_nlevel
Push eax
MoV ECx, offset STD: cout (460888 H)
Call STD: basic_ostream <char, STD: char_traits <char >:: operator <(41c686h)
MoV ECx, eax
Call STD: basic_ostream <char, STD: char_traits <char >:: operator <(41cbe0h)
Push offset string "pause" (4570c8h)
Call @ ILT + 1945 (_ system) (41c79eh)
Add ESP, 4
XOR eax, eax
Push edX
MoV ECx, EBP
Push eax
Lea edX, DS: [41e62bh]
Call @ ILT + 1540 (@ _ rtc_checkstackvars @ 8) (41c609h)
Pop eax
Pop edX
Pop EDI
Pop ESI
Pop EBX
Add ESP, 0cch
Cmp ebp, ESP
Call @ ILT + 3570 (_ rtc_checkesp) (41cdf7h)
MoV ESP, EBP
Pop EBP
RET

The red font shows that the program automatically calls the get and set methods to complete the values and values of Private Members ..

 

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.