#include <iostream>
#include <string>
template < typename T, typename V>
class Property
{
public :
typedef V ( T:: * getter_type) ( ) const ;
typedef void ( T:: * setter_type) ( const V & value) ;
Property( T * self, getter_type getter, setter_type setter) : self_( self) , getter_( getter) , setter_( setter) { }
V get( ) const { return ( self_- > * getter_) ( ) ; }
Property & set( const V & v) { ( self_- > * setter_) ( v) ; return * this ; }
operator V( ) const { return get( ) ; }
Property & operator= ( const V & v) { return set( v) ; }
Property & operator+ = ( const V & v) { return set( get( ) + = v) ; }
V * operator - > ( ) const { return & get( ) ; }
private :
T * self_;
getter_type getter_;
setter_type setter_;
} ;
class Hoge
{
public :
Hoge( ) : str( this , & Hoge:: get_str , & Hoge:: set_str ) , str_( "Hello" ) { }
Property< Hoge, std:: string > str;
std:: string get_str( ) const { return str_; }
void set_str( const std:: string & s) { str_ = s; }
private :
std:: string str_;
} ;
int main( )
{
Hoge hoge;
hoge.str + = ", World!" ;
std:: cout << hoge.str - > c_str( ) << std:: endl ;
return 0 ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgoKdGVtcGxhdGUgPHR5cGVuYW1lIFQsIHR5cGVuYW1lIFY+CmNsYXNzIFByb3BlcnR5CnsKcHVibGljOgogIHR5cGVkZWYgViAoVDo6KmdldHRlcl90eXBlKSgpIGNvbnN0OwogIHR5cGVkZWYgdm9pZCAoVDo6KnNldHRlcl90eXBlKShjb25zdCBWICZ2YWx1ZSk7CiAgUHJvcGVydHkoVCAqc2VsZiwgZ2V0dGVyX3R5cGUgZ2V0dGVyLCBzZXR0ZXJfdHlwZSBzZXR0ZXIpIDogc2VsZl8oc2VsZiksIGdldHRlcl8oZ2V0dGVyKSwgc2V0dGVyXyhzZXR0ZXIpIHt9CiAgViBnZXQoKSBjb25zdCB7IHJldHVybiAoc2VsZl8tPipnZXR0ZXJfKSgpOyB9CiAgUHJvcGVydHkgJnNldChjb25zdCBWICZ2KSB7IChzZWxmXy0+KnNldHRlcl8pKHYpOyByZXR1cm4gKnRoaXM7IH0KICBvcGVyYXRvciBWKCkgY29uc3QgeyByZXR1cm4gZ2V0KCk7IH0KICBQcm9wZXJ0eSAmb3BlcmF0b3I9KGNvbnN0IFYgJnYpIHsgcmV0dXJuIHNldCh2KTsgfQogIFByb3BlcnR5ICZvcGVyYXRvcis9KGNvbnN0IFYgJnYpIHsgcmV0dXJuIHNldChnZXQoKSArPSB2KTsgfQogIFYgKm9wZXJhdG9yIC0+KCkgY29uc3QgeyByZXR1cm4gJmdldCgpOyB9CnByaXZhdGU6CiAgVCAqc2VsZl87CiAgZ2V0dGVyX3R5cGUgZ2V0dGVyXzsKICBzZXR0ZXJfdHlwZSBzZXR0ZXJfOwp9OwoKY2xhc3MgSG9nZQp7CnB1YmxpYzoKICBIb2dlKCkgOiBzdHIodGhpcywgJkhvZ2U6OmdldF9zdHIsICZIb2dlOjpzZXRfc3RyKSwgc3RyXygiSGVsbG8iKSB7fQogIFByb3BlcnR5PEhvZ2UsIHN0ZDo6c3RyaW5nPiBzdHI7CiAgc3RkOjpzdHJpbmcgZ2V0X3N0cigpIGNvbnN0IHsgcmV0dXJuIHN0cl87IH0KICB2b2lkIHNldF9zdHIoY29uc3Qgc3RkOjpzdHJpbmcgJnMpIHsgc3RyXyA9IHM7IH0KcHJpdmF0ZToKICBzdGQ6OnN0cmluZyBzdHJfOwp9OwoKaW50IG1haW4oKQp7CiAgSG9nZSBob2dlOwogIGhvZ2Uuc3RyICs9ICIsIFdvcmxkISI7CiAgc3RkOjpjb3V0IDw8IGhvZ2Uuc3RyLT5jX3N0cigpIDw8IHN0ZDo6ZW5kbDsKICByZXR1cm4gMDsKfQo=
compilation info
prog.cpp: In member function ‘V* Property<T, V>::operator->() const [with T = Hoge, V = std::basic_string<char, std::char_traits<char>, std::allocator<char> >]’:
prog.cpp:38: instantiated from here
prog.cpp:16: warning: taking address of temporary
stdout