Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The pointers you are comparing are different, because they are pointing to different objects. Whenever you say <code>new Building</code> a new building will be created, although it my have the same name as an older building. Hence any pointer to that <code>new Building</code> will be different to any pointer to the old Building from which the new one was created. Hence the comparison is correct, the new Building is not present in the set (you just created it by copying so it cannot be present).</p> <p>You want a set, which can be compared based on a property of the object the pointer points to. For this you need to add a different comparison function which looks into the pointer to the object. Something like this should work:</p> <pre><code>struct less_building : std::binary_function&lt;std::shared_ptr&lt;Building&gt;,std::shared_ptr&lt;Building&gt;,bool&gt; { bool operator()( const std::shared_ptr&lt;Building&gt; &amp; b1, const std::shared_ptr&lt;Building&gt; &amp; b2 ) { return std::less( b1-&gt;name, b2-&gt;name ); } }; </code></pre> <p>This may need some friends declaration depending on your definition of <code>Building</code>, but in general something like this will do the trick.</p> <p><strong>NOTE1</strong>:</p> <p>Be sure to understand the implications of this, before you use this trick. Using this comparator will mean, that no two <code>Buildings</code> in your set can have the same name, irregardless of any other attribute they may have. Each pair of <code>Building</code>s which have the same name will afterwards be considered the same <code>Building</code>. Depending on your modelling domain, this may or may not be what you want.</p> <p>In case you want to also compare other attributes in case the names are the same, then you have to add this to your comparison functor. However then that means that there can be no two <code>Building</code>s which have the same set of attributes. Again, this may or may not be what you want, depending on the problem.</p> <p><strong>NOTE2</strong>: </p> <p>Usually it is very inconvenient, to mix <code>std::shared_ptr</code> and references to the same types of objects, which also leads to the problem you are experiences. If you are using <code>std::shared_ptr</code> for a type at any place, you should try to be consistent and only pass these <code>std::shared_ptr</code> around. The reason is, that converting from a <code>std::shared_ptr</code> to a reference (for example using <code>operator*()</code>) is much like a one-way function. I.e. you can get the reference from a <code>std::shared_ptr</code> but it is very hard to get back the <code>std::shared_ptr</code> from the reference. I can think of two ways to get back the <code>std::shared_ptr</code> once you decayed it to a reference, but one requires you to change the object (deriving from <code>std::enable_shared_from_this&lt;Building&gt;</code>) and the other just smells very badly of unclean code (creating a new <code>std::shared_ptr</code> with a no-op deleter to the address of the reference). So I would not recommend either ways.</p> <p>Instead either choose a design which fits your problem domain, as shown above. Or keep the <code>std::shared_ptr</code> from decaying. If you pass them around as (const)-references to the <code>std::shared_ptr</code> this will not actually cost much more than just passing around the pointer itself.</p>
    singulars
    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