It depends on what you're trying to do. First, as Kerrek SB has
commented, you don't want to use pointers if value semantics can be
applied: if Wife
is copyable and assignable, there's almost no reason
to allocate it dynamically. In this case, however, I'm guessing that
Wife
derives from Person
(although perhaps a decorator for Person
would be more appropriate, since the fact that a given Person
isA
Wife
can change over time), that there might even be types derived
from Wife
(and that Person::current_wife
might want to hold one of
these), and that in fact, Wife
has identity; you don't want copies of
the same wife all over the place.
If this is the case, then you really have to define a protocol for the
interactions of other classes with Wife
. Typically, the lifetime of
Wife
will not depend on the Person
who holds it (although if it is a
decorator, it might), so Person
should just hold a pointer to it, as
you've done. Most likely, the Wife
object will have various functions
which—implicitly or explicitly—control its lifetime: you
might have something like:
void Wife::die()
{
// ...
delete this;
}
for example. In that case, however, whoever is married to Wife
will
have to be informed, so that current_wife
doesn't point to a dead
spouse. Typically some variant of the observer pattern can be used for
this. (Note that you have exactly the same issue in Java; you don't
want Person::current_wife
to point to a dead Wife
. So you'd still
need a Wife::die()
function, and the observer pattern to notify the
spouse.)
In cases like this (which in my experience represent the vast majority
of dynamically allocated objects in C++), about the only difference
between C++ and Java is that C++ has a special syntax for calling the
“destructor”; in Java, you use the usual function call
syntax, and you can give the function any name you wish (although
dispose
seems widely used). The special syntax allows the compiler to
generate additional code to release the memory, but all of the other
activities associated with the end of the object lifetime still have to
be programmed (in the destructor, in C++—although in this case, it
might make sense to put some of them directly in the Wife::die
function).