Click here to Skip to main content
Licence 
First Posted 6 Jan 2000
Views 134,678
Bookmarked 33 times

The Microsoft VC++ Virtual Property feature

By | 6 Jan 2000 | Article
Using the __declspec(property) method to enhance legacy code

A lot of legacy C++ code exists wherein member variables are directly exposed using the public or protected keywords instead of simple accessor / mutator functions. For example consider the following simple structure definition

typedef struct tagMyStruct 
{ 
   long m_lValue1; 
   ...             // Rest of the structure definition. 
} SMyStruct; 

Scattered throughout the client code that uses this structure will be code like the following:

SMyStruct       MyStruct; 
long            lTempValue; 

MyStruct.m_lValue1 = 100;       // Or any other value that is to be assigned to it. 
... 
lTempValue = MyStruct.m_lValue1; 

Now if the module that has all the above code is suddenly required to be used in a thread safe environment, you run into a problem. Because no accessor or mutator functions exist, you cannot just put a critical section (or mutex) within the definition of SMyStruct and protect m_lValue1 or any of the other public member variables.

If it is guaranteed that you will be using the Microsoft Visual C++ compiler then there is an easy solution at hand.

Just enhance the structure 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_lInternalValue = lValue; 

      // Unlock critical section 
   } 

private: 
        long m_lInternalValue1; 

        // Define critical section member variable. 
} SMyStruct; 
That's all.

For code such as

MyStruct.m_lValue1 = 100
the compiler will automatically substitute
MyStruct.PutValue1(100)

For code like

lTempValue = MyStruct.m_lValue1
the compiler will substitute
lTempValue = MyStruct.GetValue1()

The possibilities this opens up are many. You can even use this to add reference counting to legacy structures and classes.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jeremiah Talkar



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAnybody help pls Pinmemberbeatselva20:02 14 Dec '04  
Generalmore info on __declspec(property) PinmemberTemplMetaProg10:56 3 Jul '02  
GeneralInfinte Array PinmemberFahad15:16 22 Jul '01  
GeneralRe: Infinte Array PinmemberTemplMetaProg10:54 3 Jul '02  
GeneralRe: Infinte Array Pinmemberdhumalswati0:08 23 Aug '03  
GeneralThere is a second way to do it PinsussMh2x14:42 24 Aug '00  
GeneralRe: There is a second way to do it PinmemberAli Demir22:41 8 Jan '01  
GeneralRe: There is a second way to do it PinmemberDmitry Sychov2:13 27 Aug '01  
GeneralA set of questions about this code -> PinsussAsaf Karass2:14 15 Jul '00  
GeneralRe: A set of questions about this code -> PinsussJeremiah S. Talkar4:57 17 Jul '00  
GeneralRE: Do not use this if possible PinsussLlew Goodstadt7:56 20 Jan '00  
GeneralRe: RE: Do not use this if possible PinsussJeremiah S. Talkar8:46 21 Jan '00  
GeneralRe: RE: Do not use this if possible PinmemberPhil Harding22:09 14 Mar '05  
GeneralDo not use this if possible PinsussLlew23:47 17 Jan '00  
GeneralRe: Do not use this if possible PinsussChris Maunder12:36 18 Jan '00  
GeneralRe: Do not use this if possible PinsussJeremiah S. Talkar18:17 19 Jan '00  
Generala doubt on The Microsoft VC++ Virtual Property feature PinsussVivek Medhekar22:00 17 Jan '00  
GeneralRe: a doubt on The Microsoft VC++ Virtual Property feature PinsussJeremiah S. Talkar17:33 19 Jan '00  
GeneralRe: a doubt on The Microsoft VC++ Virtual Property feature PinsussVitaly A. Brusentsev22:31 19 Jan '00  
GeneralRe: a doubt on The Microsoft VC++ Virtual Property feature PinsussJeremiah S. Talkar8:56 21 Jan '00  
GeneralRe: a doubt on The Microsoft VC++ Virtual Property feature PinsussJeremiah S. Talkar9:54 22 Jan '00  
GeneralRe: a doubt on The Microsoft VC++ Virtual Property feature PinmemberJeremiah Talkar8:05 9 Apr '01  
GeneralRe: a doubt on The Microsoft VC++ Virtual Property feature Pinmembernagip almoshaki8:32 9 Jun '02  
GeneralNice PinsussSandeep Bhatia11:14 12 Jan '00  
GeneralCool PinsussGeorge Poulose16:51 11 Jan '00  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 7 Jan 2000
Article Copyright 2000 by Jeremiah Talkar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid