class property; class base_property { public: friend class property; public: base_property (const std::string& s, const graphics_handle& h) : count (0), name (s), parent (h), hidden (false) { } virtual ~base_property (void) { } std::string get_name (void) const { return name; } graphics_handle get_parent (void) const { return parent; } bool is_hidden (void) const { return hidden; } void set_hidden (bool flag) { hidden = flag; } virtual void set (const octave_value& val) { error ("set: invalid property \"%s\"", name.c_str ()); } virtual octave_value get (void) const { error ("get: invalid property \"%s\"", name.c_str ()); return octave_value (); } base_property& operator = (const octave_value& val) { set (val); return *this; } private: int count; std::string name; graphics_handle parent; bool hidden; private: base_property (const base_property& bp) { } }; class string_property : public base_property { public: string_property (const std::string& s, const graphics_handle& h, const std::string& val = "") : base_property (s, h), str (val) { } void set (const octave_value& val) { if (val.is_string ()) str = val.string_value (); else error ("set: invalid string property value for \"%s\"", get_name ().c_str ()); } octave_value get (void) const { return octave_value (str); } std::string string_value (void) const { return str; } private: std::string str; private: string_property (const string_property& p) { } }; class property { public: property (base_property *bp) : rep (bp) { rep->count++; } property (const property& p) { rep = p.rep; rep->count++; } ~property (void) { if (--rep->count <= 0) delete rep; } std::string get_name (void) const { return rep->get_name (); } graphics_handle get_parent (void) const { return rep->get_parent (); } bool is_hidden (void) const { return rep->is_hidden (); } void set_hidden (bool flag) { rep->set_hidden (flag); } property& operator = (const octave_value& val) { *rep = val; return *this; } property& operator = (const property& p) { rep = p.rep; rep->count++; return *this; } string_property& as_string_property (void) const { return *(reinterpret_cast (rep)); } private: base_property *rep; }; //=========================================================================== class OCTINTERP_API text : public base_graphics_object { public: class properties : public base_properties { public: //... BEGIN_PROPERTIES string_property string; //... END_PROPERTIES //... }; //... }; >>>>> expands to <<<<< property string; string_property& get_string (void) const { return string.as_string_property (); } void set_string (const octave_value& val) { if (! error_state) { string = val; mark_modified (); } } >>>>> property creation <<<<< text::properties::properties (const graphics_handle& mh, const graphics_handle& p) : base_properties (go_name, mh, p), string (new string_property ("string", mh, "")), //... { //... } >>>>> property set in base_properties <<<<< class OCTINTERP_API base_properties { //... public: void add (base_property *bp) { all_props[bp->get_name ()] = property (bp); } void add (const property& p) { all_props[p.get_name ()] = p; } private: std::map all_props; }; >>>>> then the easiest is probably to rewrite properties ctor <<<<< text::properties::properties (const graphics_handle& mh, const graphics_handle& p) : base_properties (go_name, mh, p), string (new string_property ("string", mh, "")), //... { //... add (string); } Note: other solutions are possible - register property in base_property ctor, but then one need a way to get the containing properties object