Previous section   Next section

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


35.6. Virtual Properties

This simple mechanism does not allow us to have virtual properties directly, but it's very easy to provide them. As we saw with the Container::set_Capacity method, we would define a nonvirtual method that provided the correct syntax, and this would then call a virtual method. Any deriving classes would override the virtual method, and the property would act virtually.

Listing 35.31.



class Super


{


  . . .


public:


  int get_Thing() const


  {


     return get_ThingValue();


  }


public:


  method_property_get_external<int, Super, . . .>   Thing;


protected:


  virtual int get_ThingValue() const = 0;


};





class Sub


  : public Super


{


protected:


  virtual int get_ThingValue() const


  {


    . . .


  }


};





Sub   sub;


Super &super = sub;


int   i = super.Thing; // Uses Sub::get_ThingValue()



This technique can also be used to adapt existing nonvirtual read or write methods to make them compatible with the function signature requirements of the method property templates, so your property access methods simply act as shims (see Chapter 20).


      Previous section   Next section