I have two cl***** that need to synchronize with each other. That is, I
want to be able to say a = b and b = a. Would the best way to do that
be to have two friend functions that directly mess with the internals of
cl***** A and B synchronizing one with the other or vice versa? I would
like if I could to have at least a few member functions involved, but I
certainly don't want this:
/* non-friend of class A OR B! */
A& operator=(A& a, B& b) {
a.setFirstMember(b.getFirstMember());
a.setSecondMember(b.getSecondMember());
/* ...ad nauseum */
}
It would be fine if I could do it like b.save(a) and b.load(a), instead
of a = b and b = a, but I'm not sure how to do that right either. If I
made B::save and B::load take an A& as a parameter, then A would need to
be defined as a class previous to the definition of those functions:
class A {
....
};
class B {
void save(A& a);
void load(const A& a);
};
That seems fine and dandy, but for B::save and B::load to save and load
to structure A, they would need to be friends of A. Otherwise I end up
with the endless nightmare parade of getter/setter functions I mentioned
above, that I do not want. But B::save and B::load have to be defined
before A can declare itself a friend of them!
class A {
friend void B::save(A& a);
friend void B::load(A& a);
/* Uh oh, compiler's not going to like this. */
....
};
class B {
void save(A& a);
void load(A& a);
};
So what do I do? What are best practices? What works the best? All I
know is that I have to be able to store the information in an object of
class B into an object of class A, and I have to store the information
in an object of class A into class B.
[ See http://www.gotw.ca/resources/clcm.htm
for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


|