small exercise :-)
Hi
Here is the small problem that looks quite hard to crack. Task is to
compile code below without using operator== that takes C by value.
Basically -- I've been trying to implement something similar to C#
accessors. Initial plan was to make them as 'undetectable' as possible
-- i.e. moving from exposed data member to accessors (and backwards)
should not require client code change (not taking '()' into account
however).
But it looks like that C++ rules does not allow to create such object.
For example, code below compiles if you use second A::data()
declaration.
template<class T>
class accessor
{
T& m_ref;
typedef accessor<T> Self;
template<class T> friend inline accessor<T> make_accessor(T& d)
{ return accessor<T>(d); }
accessor();
accessor(T& ref) : m_ref(ref) {}
public:
// compiler generated cctor & dtor are ok
template<class C> inline operator C() { return m_ref; }
// operator==
template<class C> friend inline bool operator==(Self l, C const&
r) { return l.m_ref == r; }
template<class C> friend inline bool operator==(C const& l, Self
r) { return l == r.m_ref; }
friend inline bool operator==(Self l, Self r) { return l.m_ref ==
r.m_ref; }
};
struct B
{
int k;
B(int a = 0) : k(a) {}
operator int() {return k;} // no const deliberately!
//friend inline bool operator==(B const& l, B const& r) { return
l.k == r.k; }
//bool operator==(B const& r) const { return k == r.k; }
};
class A
{
B m_data;
public:
A(int d) : m_data(d) {}
accessor<B> data() throw() { return make_accessor(m_data); }
//B& data() throw() { return m_data; }
};
main()
{
bool volatile bval;
A a1(1);
A a2(2);
B b;
bval = a1.data() == a2.data();
bval = A(3).data() == a2.data();
bval = a1.data() == A(3).data();
bval = b == a2.data();
bval = a1.data() == b;
bval = 5 == a2.data();
bval = a1.data() == 5;
bval = a1.data() == B();
bval = B() == a2.data();
int aa = a1.data();
}
Bye.
Sincerely yours, Michael.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]