__property keyword support in GCC
Joe Buck
jbuck@synopsys.COM
Mon Dec 13 10:25:00 GMT 1999
> Should I ask anyone, whether GCC will support Borland C++ Builder's
> __property keyword and the all behind it?
The maintainers are focusing on the ISO C++ standard, so the answer is,
probably never, particularly since, as I will show, it's completely
unnecessary!
> It is very useful thing, mainly
> in the class encapsulation. For those, who aren't in touch with BC++
> Builder, i wrote small example:
>
> imagine class:
> class cStatistics
> {
> float **Data;
>
> public:
> float Average(); // computing average of stat set
> void Centrify (float); // centering stat set to new mean
> // average
> ...
> };
>
> if you want to make something with cStatistics, you have to
> declare i.e.:
> cStatistics *A = new cStatistics ();
> float AVERAGE = A->Average(); // to get average of set...
> A->Centrify (73); // if you want to set the mean average to 73
Most C++ programmers would probably set the mean with a constructor
argument, but OK.
> More ellegant way is to make it through properties:
> the same class:
> class cStatistics
> {
> ...
> public:
> __property Avg = { read = Average, write = Centrify };
>
> float Average ();
> void Centrify (float);
> ...
> };
>
> an equivalent code to the one published above:
> cStatistics *A = new cStatistics (...);
> float AVERAGE = A->Avg; // without () - treat as variable, it will call
> // cStatistics::Average() and the result will be
> // passed to read part of the property
> // function result will be presented outside class
> // as a class' variable
> A->Avg = 3; // this will call Centrify(float) with argument 3
> // altering property variable will call another f()
You don't need a language extension! Try something like
// perhaps this should be a nested class, but I'll write this way to be
// clearer.
class cStatistics;
class cStatistics_avg {
public:
cStatistics_avg(cStatistics& mainObj) : mainObj(mainObj_) {}
operator float() const { return mainObj.Average();}
void operator=(float newMean) { mainObj.Centrify(newMean);}
private:
cStatistics& mainObj;
};
class cStatistics
{
public:
float Average ();
void Centrify (float);
cStatistics_avg Avg;
};
The cStatistics constructor would need to construct its member, Avg,
by passing it a reference to *this (left as an exercise for the reader).
Now you can write the exact code you have above, and have it work with
any C++ compiler.
Furthermore you could do this as a template, so you would only have
to write code like the cStatistics_avg class above once. (left as
an exercise for the advanced programmer).
Just the same, I don't recommend this technique, as it's too "tricky".
You should want to be friendlier to the people who have to maintain your
code.
More information about the Gcc
mailing list