As mentioned in a reply to Solution 2, I doubt that it's necessary to phrase this feature as a casting operation. Why? Because you're not really casting the object to something else. You simply need access to one of its internals, which is naturally achieved through plain old member functions.
How I would do it:
class A {};
class B {};
class C
{
A a_obj;
B b_obj;
public:
A const &getA() const;
A &getA();
B const &getB() const;
B &getB();
};
int main()
{
C c1;
C const c2;
A &a1 = c1.getA();
A &a2 = c2.getA(); A const &a3 = c2.getA(); }
Notice that the integrity of the object is ensured by having the
const
members return
const
references.