Boost logo

Boost :

Subject: Re: [boost] [property] interest in C# like properties for C++?
From: Peder Holt (peder.holt_at_[hidden])
Date: 2009-10-21 13:50:07


<snip>

> I don't think that's wise at all. standard practices exist for a reason.
>
> that's like using UpperCaseNaming in the public interface of your classes,
> rather than lower_case_naming, because other languages do and you think it
> looks nicer.
> you might be right about that, but it requires everyone who uses your
> classes
> to know your syntax preferences, instead of just the standard.
> I've used properties in C# and there is no reason not to use them. in C++,
> I
> wouldn't use a C#-like properties syntax for this reason alone, even if all
> previously mentioned problems could be solved - there is an established C++
> properties syntax.
>
> std::vector<int> stl_container;
> my_container_type my_container;
>
> size1=stl_container.size();
> size2=my_container.size; //no "()"?! class author was using C# properties.
>
>
What I am missing in C++ is the grouping of the two (getting and setting)
into a logical entity. I feel this is a bit like iterators vs range. A range
is one objects that represents more than the sum of two iterators.
The same can be true of properties. In the code, it feels cleaner to group
the two together. Whether you are using one syntax or the other doesn't
matter that much to me. The important thing I would like to contribute is a
way of accessing the this pointer of the containing class. This can be
isolated into the following piece of code:

http://codepad.org/Y0z3nEk2

Or:
class my_vector {
public:
   struct {
   private:
      BOOST_PROPERTY_ACCESS_SELF(my_vector,x);
      friend class my_vector;
   public:
      const double& operator()() const {return m_x;}
      void operator()(const double& arg) {m_x=arg;self()->refresh();}
   private:
      double m_x;
   } x;

   void refresh() {std::cout <<std::endl << "Changed x to " << x() <<
std::endl;}
};

This way you are free to choose your syntax by what you choose to implement
inside your property :)

Personally I feel that the property syntax can be great, especially for GUI
programming. But I agree that it may not be fit for domain logic.

> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


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