Friday, April 23, 2010

Properties in C++?

In the never-ending quest to reduce the duplication of data I’ve been consolidating some data structures into either native structures or managed classes (mainly depending on where they’re modified the most).

While doing this I’ve run across a bit of syntax that I hadn’t seen before in native C++:

pSomeSmartPointer->SomeProperty;





I’m used to seeing properties in managed code and I suspect that the various managed C++ implementations (C++/CLI, managed extensions for C++) have something similar but this lovely expression compiled without complaint in plain old native C++.



What gives?  Native C++ doesn’t have properties does it?



The need to go digging has yielded thus:




  1. Microsoft C++ does in fact have properties.


  2. They’re defined via the __declspec keyword.


  3. The compiler translates them into the corresponding getXXX() or setXXX() methods.


  4. The #import directive takes advantage of this when importing type libraries exported from managed assemblies.  This translation from IDL to C++, gets stored in .tlh and .tli files.





See this link for an example of using properties in native C++.

No comments :

Post a Comment