Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><i>[Assuming you continue implementing your class' internals in the C-style, which may or may not be beneficial in terms of development and execution speed (depending on the whole project's design) but is generally not recommended in favor of </i><code>std::string</code><i> and friends.]</i></p> <p>Turning</p> <pre><code>const char *filename; </code></pre> <p>into</p> <pre><code>char *filename; </code></pre> <p>will not make you happy with the <code>strcpy</code>, since you actually need some memory for a copy of your string :)</p> <p>For the manual memory management code part, please see Tadeusz Kopec's answer, which seems to have it all right.</p> <p>Also, keep in mind that there is a difference between</p> <pre><code>const char *filename; // "filename" points to "const char" // and is not const itself char const *filename; // semantically the same as above </code></pre> <p>and</p> <pre><code>char * const filename; // "filename" is const and points to "char", // which is not const </code></pre> <p>In the first case, you can make <code>filename</code> point to any other <code>const char</code> string, in the second, you can only change that string "in-place" (so keeping the <code>filename</code> value the same, as it points to the same memory location). Of course one can combine these two (or none of them) if needed.</p> <p>P.S. If you name your member function's parameter <code>_filename</code> only to avoid naming collision with the member variable <code>filename</code>, you can just prefix it with <code>this</code> (and get rid of the underscore):</p> <pre><code>void MyClass::func (const char *filename) { ... this.filename = copy; } </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. 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