Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>First is that, why is const needed before char? or why is it there?. Also, why is it declaring as a pointer?</p> </blockquote> <p>I'll start by answering your last question first. This is declared as a pointer so the constructor can accept an array of characters. The <code>const</code> is there to indicate that the characters being pointed to are not modified inside this <code>bozo</code> constructor. This is important because your example usage can pass in C-style string literals which typically reside in read-only memory portion of your program. Any attempts to modify such a string literal is undefined behavior.</p> <blockquote> <p>Second, is there any difference between the "primary form" and the "short form?"</p> </blockquote> <p>The first form is creating a temporary <code>Bozo</code> object on the right-hand side and that object is used to initialize <code>bozetta</code> on the left-hand side. Now you might think that an extra copy happens with this form but that isn't the case. The C++ standard is worded in such a way so that it permits the compiler to skip this unnecessary extra copy during construction by <em>eliding</em>. So in practice, you'll find that on all modern C++ compilers you try this on, <code>bozetta</code> will be constructed using the prototype you showed above -- no extra copying happens.</p> <p>The second form explicitly constructs a <code>Bozo</code> object using a <code>Bozo</code> constructor that can accept two string literals as parameters.</p> <p>These two forms have the same behavior assuming that constructor isn't declared <code>explicit</code>.</p> <blockquote> <p>I thought char can only contain a single alphabet, and it's not a char array.</p> </blockquote> <p>That is correct, a <code>char</code> can only hold one byte worth of information. C and C++ doesn't have an actual native string type, not like the type you're use to like in other languages. So a "string" is really just a bunch of <code>chars</code> laid out in a contingent block of memory. Your <code>Bozo</code> constructor above can work with this block by taking a pointer to where that block starts as input.</p> <blockquote> <p>Could I do this with string instead?</p> </blockquote> <p>Yes you can and that is actually the preferred way you should do this by using <code>std::string</code>. The constructor declaration would look like this:</p> <pre><code>Bozo(std::string fname, std::string lname); // or to avoid potential copying of long strings Bozo(const std::string &amp;fname, const std::string &amp;lname); </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