Boost logo

Boost Users :

From: Gennadiy Rozental (gennadiy.rozental_at_[hidden])
Date: 2004-10-22 16:32:59


> >I guess the functionality he wants is like the property keyword in VC++.
> >Unfortunately this is a Microsoft only extension.
>
> This is acutally stolen by Microsoft from Borland Delphi's Object Pascal.
>
> I really liked the feature and wished C++ had a solution. I haven't spent
any
> time thinking about adding it, or how to.
>
> >Any ideas? Suggestions?
> >
> >Thanks

Boost.Test library contain header class property that implement AFAICT the
solution to your problem:

Example usage like this:

Example1:

class A{
public:
    A( std::string name ) : p_name( name ) {}
    readonly_property<std::string> p_name;
};

...
A a;

std::cout << a.p_name;
or
std::string s( a.p_name );

a.p_name = "abc"; <-- this will produce error
a.p_name.value = "abc"; <-- this will produce error

Example2:

class A{
public:
    readwrite_property<int> p_id;
};

...
A a;

std::cout << a.p_id;

a.p_id.value = 1;

Example3:

class A {
public:
    BOOST_READONLY_PROPERTY( float, (B)(C) ) p_highwatermark;
};

This is an example of readonly property that only accessible for
modification to the methods of classes B and C

HTH,

Header is here:

boost/test/detail/class_properties.hpp

(Old version is here:
http://www.boost.org/boost/test/detail/class_properties.hpp, but I
recommend to use new from cvs or one that will come with next release)

Gennadiy


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net