thanks,
if the situation is so:
class B{
A *a1;
public:
B(B &b1){???}
This case is trickier since a1 is now a pointer. The question is
whether to make a "deep copy" or a "shallow copy" of B. A deep copy
also creates a copy of the A object that a1 points to - and then
assigns the copied A object's address to the a1 member pointer in B's
copy. A shallow copy would just copy the a1 pointer itself - meaning
that both the original and copied B object would each have an a1 member
pointing to the same A object - which can be a problem if B is supposed
to delete the A object at some point (more on that problem later).
In terms of advantages, though, a shallow copy is more efficient and
economical than making a deep copy. Furthermore, a shallow copy of B
looks easy enough to implement:
class B
{
A *a1;
public:
B( B& b1)
{
a1 = b1.a1;
}
};
....but as mentioned above can be very difficult to maintain correctly,
now that a1 is shared. So to manage a shallow copy safely, B should
turn its a1 member into a "smart pointer" such as
std::tr1::shared_ptr<A>.
Note that "a1", even as a pointer member, can still be initialized the
same way as before (in the member list initializer of the constructor)
instead of within the body of B's constructor - as shown here. When
possible, it's better to initialize a class member in the constructor's
member-initializer list than in its body.
Greg