Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>Wife</code> is copyable and assignable, there's almost no reason to allocate it dynamically. In this case, however, I'm guessing that <code>Wife</code> derives from <code>Person</code> (although perhaps a decorator for <code>Person</code> would be more appropriate, since the fact that a given <code>Person</code> isA <code>Wife</code> can change over time), that there might even be types derived from <code>Wife</code> (and that <code>Person::current_wife</code> might want to hold one of these), and that in fact, <code>Wife</code> has identity; you don't want copies of the same wife all over the place.</p> <p>If this is the case, then you really have to define a protocol for the interactions of other classes with <code>Wife</code>. Typically, the lifetime of <code>Wife</code> will not depend on the <code>Person</code> who holds it (although if it is a decorator, it might), so <code>Person</code> should just hold a pointer to it, as you've done. Most likely, the <code>Wife</code> object will have various functions which&mdash;implicitly or explicitly&mdash;control its lifetime: you might have something like:</p> <pre><code>void Wife::die() { // ... delete this; } </code></pre> <p>for example. In that case, however, whoever is married to <code>Wife</code> will have to be informed, so that <code>current_wife</code> 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 <code>Person::current_wife</code> to point to a dead <code>Wife</code>. So you'd still need a <code>Wife::die()</code> function, and the observer pattern to notify the spouse.)</p> <p>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 &ldquo;destructor&rdquo;; in Java, you use the usual function call syntax, and you can give the function any name you wish (although <code>dispose</code> 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++&mdash;although in this case, it might make sense to put some of them directly in the <code>Wife::die</code> function).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload