Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like you get the main idea, but there's a couple things worth mentioning.</p> <p>1) As @chris mentioned, you're forgetting your copy constructor, copy assignment operator, move constructor, and move assignment operator. Copy should be either manually written or disabled, move can be defaulted. (aka You've not followed the rule of 5)</p> <p>2) Prefer to use <code>std::unique_ptr</code> to represent ownership. It's already done all of the hard work for you. By keeping your wrapped string in a <code>std::unique_ptr</code> the default versions of the copy/move special functions will preserve correctness (though you'll still want to implement the copy operations if you want them to be enabled).</p> <p>Here's what this might look like:</p> <pre><code>class StringGetter { public: explicit StringGetter(char* name) { strSize = strlen(name); str = std::unique_ptr&lt;char[]&gt;(new char(strSize+1)); std::copy_n(name, strSize + 1, str.get()); } StringGetter(const StringGetter&amp; other) { strSize = other.strSize; str = std::unique_ptr&lt;char[]&gt;(new char(strSize+1)); std::copy_n(other.str.get(), strSize + 1, str.get()); } StringGetter(StringGetter&amp;&amp; other) = default; StringGetter&amp; operator=(const StringGetter&amp; rhs) { auto temp = rhs; swap(temp); return *this; } StringGetter&amp; operator=(StringGetter&amp;&amp; rhs) = default; const char* getString() const noexcept { return str.get(); } void swap(StringGetter&amp; other) { std::swap(str, other.str); std::swap(strSize, other.strSize); } private: std::unique_ptr&lt;char[]&gt; str; int strSize; }; </code></pre> <p>Miscellaneous items:</p> <p>1) Note that <code>std::unique_ptr</code> handles the RAII. When I replace 'str' in the copy constructor, it deletes the old string automatically.</p> <p>2) I size the dynamically allocated memory to match the input string. This prevents overflows/wasted memory.</p> <p>3) The constructor is <code>explicit</code>. This prevents undesirable conversions. Rule of thumb is to use the <code>explicit</code> keyword on all single argument constructors.</p> <p>4) I made <code>getString</code> constant so that you can still call it on <code>const</code> instances of the class.</p> <p>5) I replaced the str-family copy function with <code>std::copy_n</code>. It's more general and can avoid certain pitfalls in obscure cases.</p> <p>6) I used the copy-swap idiom in the copy assignment operator. This promotes code reuse/avoids duplication.</p> <p>7) When C++14 comes out, replace the <code>std::unique_ptr</code> constructor call with <code>std::make_unique</code> for added exception-safety and to remove redundancy.</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. 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