Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Having two objects at the same address would mean that there would be no way to distinguish between these two objects when referencing them with pointers. For example, in the following code:</p> <pre><code>EmptyClass o1; EmptyClass o2; EmptyClass * po = &amp;o; po-&gt;foo(); </code></pre> <p>Should the foo method be called on <code>o1</code> or <code>o2</code>?</p> <p>It could be argued that since these objects have no data and no virtual methods (otherwise they would have a non-zero size), it doesn't matter on which instance the method is invoked. However, this becomes more important when we want to test if two objects are equal (i.e. if they are the same):</p> <pre><code>template &lt; typename T &gt; bool isSame( T const &amp; t1, T const &amp; t2 ) { return &amp;t1 == &amp;t2; } EmptyClass o1; // one object and... EmptyClass o2; // ...a distinct object... assert( ! isSame( o1, o2 ) ); // ...should not be one and same object! </code></pre> <p>For a more concrete example, let's assume that I want to associate some objects (I could say entities) with some values, let's say in an associative container:</p> <pre><code>Person you; Person me; // You and I are two different persons // (unless I have some dissociative identity disorder!) // Person is a class with entity semantics (there is only one 'me', I can't make // a copy of myself like I would do with integers or strings) std::map&lt; Person *, std::string &gt; personToName; personToName[&amp;you] = "Andrew_Lvov"; personToName[&amp;me] = "Luc Touraille"; // Oh, bother! The program confused us to be the same person, so now you and I // have the same name! </code></pre> <p>So yes, it all boils down to object identity: if objects were allowed to be empty, they could be deprived of their identity, which is simply not allowed by the language (thankfully).</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. 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.
 

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