Previous section   Next section

Imperfect C++ Practical Solutions for Real-Life Programming
By Matthew Wilson
Table of Contents
Chapter 35.  Properties


35.1. Compiler Extensions

Although properties are not part of the language at this time, a few compilers do provide them as an extension (e.g., Borland C++'s __property and Intel / Visual C++'s __declspec(property)), as in:

Listing 35.3.


class Date


{


public:


  WeekDay get_DayOfWeek() const;


  . . .


#if defined(ACMELIB_COMPILER_IS_BORLAND)


  __property WeekDay DayOfWeek = {read = get_DayOfWeek};


#elif defined(ACMELIB_COMPILER_IS_INTEL) || \


      defined(ACMELIB_COMPILER_IS_MSVC)


  __declspec(property(get = get_DayOfWeek)) WeekDay DayOfWeek;


#else


# error Property extensions not supported by your compiler


#endif // compiler


  . . .


};



Obviously the main problem is that property extensions are nonstandard, and only available on a few compilers. Even those that do support them, support only instance properties, and not class (static) properties.[2] We need something more powerful and more portable.

[2] Visual C++ helpfully suggests that "property methods can only be associated with non-static data members." Borland C++ (5.6) has an internal compiler error.


      Previous section   Next section