It’s time for my first question now. How do you cross assignments operators between two classes?
class B; class A { public: A &operator = ( const B &b ); friend B &B::operator = ( const A &a ); //compiler error }; class B { public: B &operator = ( const A &a ); friend A &A::operator = ( const B &b ); };
I searched for how to forward declare a member function like:
class B; B &B::operator = ( const A &a ); //error
But I didn’t find anything. And I don’t want to make the classes all-out friends with each other. How do I do this?
The reason for the compiler error is a circular dependency. Each of your operator=() functions require knowledge of the operator=() function inside the other class, so no matter which order you define your classes in, there will always be an error.
Here is one way to sort it out. It isn’t very elegant, but it will do what you want:
You may also be able to solve this problem with inheritance.
edit: here is an example using inheritance. This will work if the copying procedure only needs access to some common data shared by both A and B, which would seem likely if the = operator is to have any meaning at all.