Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to store a string, you can store a <code>std::string</code>:</p> <pre><code>class A{ private: std::string a; int b; public: A(std::string const&amp;, int); } A::A(std::string const&amp; _a, int _b) : a(_a), b(_b) {} </code></pre> <p>If your instructor insists that you treats pointers to characters as strings, then you have a complicated matter on your hand, because you have to deal with managing the memory that character points to by itself, lest you end up <a href="https://stackoverflow.com/questions/8839943/why-is-it-a-memory-leak-what-could-i-catch-if-i-shall-use-such-things-in-c/8840302#8840302">leaking memory</a>. In other words, you need to implement something similar to <code>std::string</code>.</p> <p>So, assuming you really need to do this...</p> <ul> <li>Whenever you use <code>new</code> you need a matching <code>delete</code> to release the memory allocated by <code>new</code>;</li> <li>You probably want <code>new</code> to be called by the constructor of a class;</li> <li>You can get <code>delete</code> done for you automatically if use objects of automatic storage duration of a class with a proper destructor that does that clean up;</li> <li>That class can either be made non-copyable (e.g. by making a private copy constructor and copy assignment operator), or made copyable if you write <a href="https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three">proper copy constructor</a> and <a href="https://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom">copy assignment operator</a>;</li> <li>You can easily make copies using the <code>std::copy</code> algorithm, or, if that is restricted as well for arbitrary reasons, you can probably use the old <code>memcpy</code> or <code>strcpy</code> functions.</li> </ul> <p>This is not very simple, but if you do this you will have a pretty handy class that manages the memory automatically for you, leaving you free to focus on other issues.</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.
 

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