I try to implement C++ properties as templates as defined in WikiPedia
template <typename T> class property {
T value;
public:
T & operator = (const T &i) {
::std::cout << i << ::std::endl;
return value = i;
}
// This template class member function template serves the purpose to make
// typing more strict. Assignment to this is only possible with exact identical
// types.
template <typename T2> T2 & operator = (const T2 &i) {
::std::cout << "T2: " << i << ::std::endl;
T2 &guard = value;
throw guard; // Never reached.
}
operator T const & () const {
return value;
}
};
Now suppose I declare 2 classes, one of which contains the other as a property:
class A
{
public:
Property<double> pA1;
Property<double> pA2;
};
class B
{
public:
Property<A> pB1;
Property<double> pB2;
};
Now, is there a way to declare a B and access properties of A in it?
B b;
b.pB1.pA1=1;
does not work and;
((A) b.pB1).pA1=1;
works w/o error but does not actually change the actual A of B, because accesing ((A) b.pB1).pA1 gives unchanged value, since it probably makes a copy.
Is there a way to do it w/o pointers?