Re: C# Properties, inner classes and CRTP
stroumpf2004-gtalk@yahoo.de wrote:
A primary goal of inheritance is to avoid code redundancy (that causes
more trouble than just work, btw).
Ok, if I understand correctly fragmental classes are there to *inject*
code, not to establish OOP type relationships (typical "is-a"
relationships established by inheritance). This leads me to think that
the use of inheritance is the wrong mechanism then.
It seems the problem can be solved by the concept proposal for C++0x:
template <typename T>
concept Property <P> {
typedef T value_type;
// All P must implement:
const T& P::get () const;
void P::set (const T&);
// Default operations (these are *injected*):
P& P::operator = (const T& v) {set (v); return *this;}
const T& P::operator T () {return get ();}
}
Usage:
class C {
float angle_;
public:
struct Angle {
void set (float f) {...}
float get() {...}
} angle;
concept_map <float> Property <Angle>;
};
This is a more regular method of injecting code (or formally "add an
interface" to a class). Having a third way --- fragmental classes ---
seems unnecessary.
Also, why the need for enclosing_object and offsetof tricks? It seems
to me that, in the common case at least, you can just as well just
store the implementation data in the property itself:
class C {
public:
struct Angle {
void set (float f) {...}
float get() {...}
private: float value;
friend C; // Give access to enclosing class (optional).
} angle;
concept_map <float> Property <Angle>;
};
Now implement the enclosing class in terms of (angle.value) if access
is needed, or plain (angle) to go through the Property interface as
outside clients must do.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]