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 ..