Boost logo

Boost :

Subject: Re: [boost] [property] interest in C# like properties for C++?
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2009-10-22 15:54:18


Hi,
----- Original Message -----
From: "David Brownstein" <dbrownstein_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, October 22, 2009 6:22 PM
Subject: Re: [boost] [property] interest in C# like properties for C++?

>
> The only real difference that I see between assignment-style and
> function-style accessors is that if you use function-style accessors and
> later you replace an automatically generated accessor with a customized
> (manually generated) accessor, then all of the code that uses the original
> accessor continues to work (with a recompile).
>
> For example:
>
> struct MyClass
> {
> Property<int> counter;
> };
>
> void foo()
> {
> MyClass x;
>
> x.counter( 5 );
> }
>
> Now let's say that we re-define MyClass:
>
> struct MyClass
> {
> void counter( int iValue ) { counter = iValue; NotifyObserver(); }
>
> private:
> int counter;
> };
>
> The function foo() doesn't need to be changed. This is more difficult to do
> with assignment-style accessors.

Well, this is true in this particular case. But if you think that you encapsulates the class MyClass when the Property class provides function like accessors you are wrong. Let me show you why.

Don't forget, public is public, so part of the contract. If you want to encapsulate you need to make the fields private. The property class give you the illusion of providing encapsulation to MyClass, but as counter is in the public interface, the user can take the address of this field,

struct MyClass
{
    Property<int> counter;
};

MyClass a;

Property<int>* pt = &a.counter;

play now with pt.

As you can see you can not modify the class MyClass without impacts on the code as far as you modify its public part.

Best,
Vicente


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk