Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not sure I understand all aspects of your question. When you say a private member class, do you mean private member variable? Or is it privately inheriting? I don't understand "when I try to access my string class object with this, it points to the Parent class".</p> <p>You're right, inheriting from std::string is probably not a very good idea. First of all, making it a member of a derived string requires that you know quite a lot about the underlying implementation; this may change from distribution to distribution making the code non-portable. If you do write an implementation that is portable, using the already-defined interface provided by std::string, you won't be able to take advantage of any real optimization anyway. Unless you have a really good reason for this, you're better off not doing it at all.</p> <p>Second, the name "add" is probably not the best, as it doesn't seem to describe what you're doing. "surround" may be a better name.</p> <p>I think an external function like this might be better, avoiding the whole idea of inheriting from string:</p> <pre><code>void surround(std::string &amp;orig, std::string const &amp;pre, std::string const &amp;post) { orig = pre + orig + post; } </code></pre> <p>or, if you want higher performance, do something like this:</p> <pre><code>void surround(std::string &amp;orig, std::string const &amp;pre, std::string const &amp;post) { std::string str; str.reserve(orig.size() + pre.size() + post.size()); str.insert(str.end(), pre.begin(), pre.end()); str.insert(str.end(), orig.begin(), orig.end()); str.insert(str.end(), post.begin(), post.end()); std::swap(str, orig); } </code></pre>
    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.
 

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