Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Trying to collect some uses:</p> <p><strong>Binding some temporary to reference-to-const, to lengthen its lifetime.</strong> The reference can be a base - and the destructor of it doesn't need to be virtual - the right destructor is still called:</p> <pre><code>ScopeGuard const&amp; guard = MakeGuard(&amp;cleanUpFunction); </code></pre> <p><em>Explanation</em>, using code:</p> <pre><code>struct ScopeGuard { ~ScopeGuard() { } // not virtual }; template&lt;typename T&gt; struct Derived : ScopeGuard { T t; Derived(T t):t(t) { } ~Derived() { t(); // call function } }; template&lt;typename T&gt; Derived&lt;T&gt; MakeGuard(T t) { return Derived&lt;T&gt;(t); } </code></pre> <p>This trick is used in Alexandrescu's ScopeGuard utility class. Once the temporary goes out of scope, the destructor of Derived is called correctly. The above code misses some small details, but that's the big deal with it. </p> <hr> <p><strong>Use const to tell others methods won't change the logical state of this object.</strong></p> <pre><code>struct SmartPtr { int getCopies() const { return mCopiesMade; } }; </code></pre> <hr> <p><strong>Use const for copy-on-write classes</strong>, to make the compiler help you to decide when and when not you need to copy.</p> <pre><code>struct MyString { char * getData() { /* copy: caller might write */ return mData; } char const* getData() const { return mData; } }; </code></pre> <p><em>Explanation</em>: You might want to share data when you copy something as long as the data of the originally and the copie'd object remain the same. Once one of the object changes data, you however need now two versions: One for the original, and one for the copy. That is, you <em>copy</em> on a <em>write</em> to either object, so that they now both have their own version. </p> <p><em>Using code</em>:</p> <pre><code>int main() { string const a = "1234"; string const b = a; // outputs the same address for COW strings cout &lt;&lt; (void*)&amp;a[0] &lt;&lt; ", " &lt;&lt; (void*)&amp;b[0]; } </code></pre> <p>The above snippet prints the same address on my GCC, because the used C++ library implements a copy-on-write <code>std::string</code>. Both strings, even though they are distinct objects, share the same memory for their string data. Making <code>b</code> non-const will prefer the non-const version of the <code>operator[]</code> and GCC will create a copy of the backing memory buffer, because we could change it and it must not affect the data of <code>a</code>!</p> <pre><code>int main() { string const a = "1234"; string b = a; // outputs different addresses! cout &lt;&lt; (void*)&amp;a[0] &lt;&lt; ", " &lt;&lt; (void*)&amp;b[0]; } </code></pre> <hr> <p><strong>For the copy-constructor to make copies from const objects and temporaries</strong>:</p> <pre><code>struct MyClass { MyClass(MyClass const&amp; that) { /* make copy of that */ } }; </code></pre> <hr> <p><strong>For making constants that trivially can't change</strong></p> <pre><code>double const PI = 3.1415; </code></pre> <hr> <p><strong>For passing arbitrary objects by reference instead of by value</strong> - to prevent possibly expensive or impossible by-value passing</p> <pre><code>void PrintIt(Object const&amp; obj) { // ... } </code></pre>
    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. 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