<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Eric Fleegal's WebLog : C++ Programming</title><link>http://blogs.msdn.com/ericflee/archive/tags/C_2B002B00_+Programming/default.aspx</link><description>Tags: C++ Programming</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>How to do Object Properties in C++ </title><link>http://blogs.msdn.com/ericflee/archive/2008/04/17/how-to-do-object-properties-in-c.aspx</link><pubDate>Fri, 18 Apr 2008 01:03:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8404867</guid><dc:creator>ericflee</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/ericflee/comments/8404867.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericflee/commentrss.aspx?PostID=8404867</wfw:commentRss><description>&lt;DIV&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;One of the many useful features of modern languages like C# are &lt;A href="http://en.wikipedia.org/wiki/Property_(programming)"&gt;&lt;FONT color=#0066a7&gt;object properties&lt;/FONT&gt;&lt;/A&gt;&lt;/FONT&gt;&lt;FONT size=3&gt;&lt;FONT color=#000000&gt;, as they provide a higher level of encapsulation than public fields.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;The field-like syntax is far easier to read and write than traditional C++ Get&lt;I&gt;XXX&lt;/I&gt; and Set&lt;I&gt;XXX&lt;/I&gt; functions.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;&lt;/FONT&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;It’s surprising how many people don’t know that Visual C++ has properties too.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Microsoft added property fields into C++ as a language extension back in the days when &lt;A href="http://en.wikipedia.org/wiki/Component_Object_Model"&gt;&lt;FONT color=#0066a7&gt;COM programming&lt;/FONT&gt;&lt;/A&gt;&lt;/FONT&gt;&lt;FONT color=#000000 size=3&gt; was all the rage.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;As with most C++ language extensions, the syntax is a bit clumsy; this one uses a Microsoft specific __declspec compiler directive.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;The syntax is:&lt;/FONT&gt; 
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;__declspec ( property ( get=&lt;I&gt;nameOfGetFunction&lt;/I&gt;, put=&lt;I&gt;nameOfSetFunction&lt;/I&gt; ) ) &lt;I&gt;typeExpressing propertyName&lt;/I&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT size=3&gt;&lt;FONT color=#000000&gt;When the compiler sees a data member declared with this attribute on the right of a member-selection operator ("." or "-&amp;gt;"), it converts the operation to a get or put function, depending on whether such an expression is an l-value or an r-value. In more complicated contexts, such as "+=", a rewrite is performed by doing both get and put.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;A property can also be declared read-only or write-only by specifying only the get or put function respectively.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;I&gt;&lt;FONT color=#000000 size=3&gt;&lt;/FONT&gt;&lt;/I&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;To make life a little easier, I introduce a header file “C++ Properties.h” with the following macros:&lt;/FONT&gt; 
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;#define PROPERTY(TYPE, NAME) __declspec(property(get=Get##NAME,put=Set##NAME)) TYPE NAME&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;#define READONLY_PROPERTY(TYPE, NAME) __declspec(property(get=Get##NAME)) TYPE NAME&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;#define WRITEONLY_PROPERTY(TYPE, NAME) __declspec(property(put=Set##NAME)) TYPE NAME&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;Notice that these macros use preprocessor token pasting so that the get and put functions always map to Get&lt;I&gt;XXX &lt;/I&gt;and Set&lt;I&gt;XXX&lt;/I&gt;, where &lt;I&gt;XXX&lt;/I&gt; is the name of the property.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;This allows us to declare classes with properties in a very readable form; for example:&lt;/FONT&gt; 
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;class GamePad&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;{&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;public:&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;READONLY_PROPERTY(bool, IsConnected);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;bool GetIsConnected() const;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;While not quite as elegant as the built in property syntax in C#, it’s not a bad substitute.&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;&lt;/FONT&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;You can declare virtual C++ properties simply by making the getter and/or setter methods virtual.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Similarly, abstract properties can be defined by using pure virtual getter and/or setter methods.&amp;nbsp; For example:&lt;/FONT&gt; 
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;class GamePad&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;{&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;public:&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;READONLY_PROPERTY(bool, IsConnected);&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;// virtual property&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;virtual bool GetIsConnected() const;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;&lt;/FONT&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;READONLY_PROPERTY(float, PollingRate);&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;// abstract property&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;virtual float GetPollingRate() const = 0;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;Note that the const semantics for the property are determined by the getter or setter method.&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;&lt;/FONT&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;Array semantics are also supported. The syntax is basically the same, but with an added “[]”, as follows:&lt;/FONT&gt; 
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;__declspec ( property ( get=&lt;I&gt;nameOfGetFunction&lt;/I&gt;, put=&lt;I&gt;nameOfSetFunction&lt;/I&gt; ) ) &lt;I&gt;typeExpressing propertyName&lt;/I&gt;&lt;FONT color=#ff0000&gt;[]&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;The accessor function simply needs to take an index argument.&amp;nbsp; &lt;/FONT&gt;&lt;FONT color=#000000 size=3&gt;Although we can use our existing macros for arrays, as follows&lt;/FONT&gt; 
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;class GamePad&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;{&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;public:&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;READONLY_PROPERTY(ButtonState, Buttons)&lt;FONT color=#ff0000&gt;[]&lt;/FONT&gt;;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;ButtonState GetButtons(size_t buttonIndex);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;};&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;&lt;/FONT&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;FONT color=#000000&gt;//&amp;nbsp;&lt;/FONT&gt;&lt;FONT color=#000000&gt;And used like:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;GamePad gamePad;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;. . .&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;ButtonState buttonState = gamepad.Buttons[3];&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;I find it somewhat less confusing to have additional macros in “C++ Properties.h”&lt;/FONT&gt; 
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;#define ARRAY_PROPERTY(TYPE, NAME) __declspec(property(get=Get##NAME,put=Set##NAME)) TYPE NAME[]&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;#define READONLY_ARRAY_PROPERTY(TYPE, NAME) __declspec(property(get=Get##NAME)) TYPE NAME[]&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;#define WRITEONLY_ARRAY_PROPERTY(TYPE, NAME) __declspec(property(put=Set##NAME)) TYPE NAME[]&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;Changing the above class into:&lt;/FONT&gt; 
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;class GamePad&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;{&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;public:&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;B&gt;READONLY_ARRAY_PROPERTY&lt;/B&gt;(ButtonState, Buttons);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;ButtonState GetButtons(size_t buttonIndex);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;The array access functions can also be multidimensional:&lt;/FONT&gt; 
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;class Picture&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;{&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;public:&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;READONLY_ARRAY_PROPERTY(Color, Pixels);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Color GetPixels(unsigned int x, unsigned int y);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;};&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;&lt;/FONT&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;// And used like:&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;Picture picture;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;. . . &lt;/FONT&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;Color colorAt = picture.Pixels[x][y];&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;Because Properties provide a higher level of encapsulation than public fields, I often find myself exposing private fields through const properties. &lt;/FONT&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;class GamePad&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;{&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;public:&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;READONLY_PROPERTY(bool, IsConnected);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;bool GetIsConnected() const { return isConnected_; }&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;. . .&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;private:&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;bool isConnected_;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;};&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;Just like traditional accessor functions, this enables internal members to change the &lt;FONT face="Courier New"&gt;isConnected_&lt;/FONT&gt; state while exposing the state to the public scope as a const property.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;This pattern is so very common that I introduce an explicit property macro for it:&lt;/FONT&gt; 
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;#define READONLY_PROPERTY_RVALUE(TYPE, NAME, RVALUE_EXPR) \&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;__declspec(property(get=Get##NAME)) TYPE NAME; \&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;TYPE Get##NAME() const { return RVALUE_EXPR; }&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;For symmetry I also add the following two macros, though admittedly they’re rarely used (and many of my colleagues hate them).&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;&lt;/FONT&gt;
&lt;BLOCKQUOTE dir=ltr style="MARGIN-RIGHT: 0px"&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;#define PROPERTY_VALUE(TYPE, NAME, RVALUE_EXPR, LVALUE_EXPR) \&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;__declspec(property(get=Get##NAME,put=Set##NAME)) TYPE NAME; \&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;TYPE Get##NAME() const { return RVALUE_EXPR; } \&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;void Set##NAME(TYPE newValue) { LVALUE_EXPR = newValue; }&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;&lt;/FONT&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;#define WRITEONLY_PROPERTY_LVALUE(TYPE, NAME, LVALUE_EXPR) \&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;__declspec(property(put=Set##NAME)) TYPE NAME; \&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt; TEXT-INDENT: 0.5in"&gt;&lt;FONT face="Courier New" color=#000000 size=2&gt;void Set##NAME(TYPE newValue) { LVALUE_EXPR = newValue; }&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;Although it would&amp;nbsp;preferable for C++ properties to have a cleaner built-in&amp;nbsp;syntax, these macros provide enough of an abstraction to enable use of properties without sacrificing readability.&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT color=#000000 size=3&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8404867" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericflee/archive/tags/C_2B002B00_+Programming/default.aspx">C++ Programming</category></item><item><title>#pragma once</title><link>http://blogs.msdn.com/ericflee/archive/2008/04/17/pragma-once.aspx</link><pubDate>Fri, 18 Apr 2008 01:02:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8404862</guid><dc:creator>ericflee</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/ericflee/comments/8404862.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ericflee/commentrss.aspx?PostID=8404862</wfw:commentRss><description>&lt;DIV&gt;
&lt;P style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri color=#000000 size=3&gt;Most C++ compilers now support the non-standard &lt;A href="http://en.wikipedia.org/wiki/Pragma_once"&gt;&lt;FONT color=#0066a7&gt;#pragma once&lt;/FONT&gt;&lt;/A&gt; compiler directive.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;This directive instructs the compiler to #include the file only once in a single compiland, and replaces the old C-style header sentinels (often called &lt;A href="http://en.wikipedia.org/wiki/Include_guard"&gt;&lt;FONT color=#0066a7&gt;#include guards&lt;/FONT&gt;&lt;/A&gt;).&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri color=#000000 size=3&gt;The central problem with preprocessor based header sentinels is that&amp;nbsp;they require the user to create a unique symbol to identify each and every header file that might be #included by a single compiland.&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;On very large projects, this burden becomes somewhat painful.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/SPAN&gt;Consider also the distinct possibility that two libraries might contain public header files with the exact same name; using the typical __FILENAME_H__ convention, its very possible to run into name collision between the two libraries.&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Some project teams attempt to avoid this problem by imposing a strict sentinel naming standard, usually including a file's path as part its sentinel name.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;I'm not fond&amp;nbsp;of this solution because if a header file needs to be moved, as occurs when refactoring a library, it requires that the header file be edited to conform to its new location.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;The #pragma once directive avoids all this nonsense entirely.&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri color=#000000 size=3&gt;A secondary problem is one of efficiency.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;The #pragma once directive allows the compiler to avoid opening and preprocessing a header file after its been seen once.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Although it is technically possible for a compiler to implement a similar mechanism when it encounters the header sentinel pattern (GCC can do this for instance), the mechanism is a bit fragile because it depends on a user following an exact coding pattern for the optimization to work correctly.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;I’ve encountered code patterns in library header files that appear to be the header-sentinel pattern, but are in fact not.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Moreover, the compiler must account for the fact that preprocessor symbols can be explicitly #undef’d.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;I much prefer the explicit directive because it states exactly the intention of the programmer -- "include this header only once".&lt;/FONT&gt; 
&lt;P style="MARGIN: 0in 0in 10pt"&gt;&lt;FONT face=Calibri color=#000000 size=3&gt;Unfortunately for users of GCC, this compiler directive has been deprecated (although it’s still supported last time I checked).&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;It’s my &lt;I&gt;personal&lt;/I&gt; opinion that this is yet another case of Gnu’s pervasive NIH syndrome (they got it bad), however the official reason is that the construct is not portable (which I admit is technically true).&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;I do not understand why the standards committee hasn’t adopted it into the ISO standard.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;It’s a trivial compiler feature to implement.&amp;nbsp; Although its not an official language feature, &lt;EM&gt;most&lt;/EM&gt; C/C++ compilers support it.&lt;/FONT&gt;&lt;/P&gt;&lt;/DIV&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8404862" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ericflee/archive/tags/C_2B002B00_+Programming/default.aspx">C++ Programming</category></item></channel></rss>