Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>struct myObj { //... char myVarChar; }; </code></pre> <p>This won't work. You either need a fixed size array, a pointer to char or use the struct hack. You won't be able to assign a pointer to this <code>myVarChar</code>.</p> <blockquote> <p>so i'd like to create my own lightweight string class which has no extra overhead std::string has.</p> </blockquote> <p>What extra overhead are you referring to? Have you tested and measured to see if <code>std::string</code> is really a bottleneck?</p> <blockquote> <p>I think std::string is represented contiguously</p> </blockquote> <p>Yes, mostly, the character buffer part. However, the following:</p> <blockquote> <p>(int)length(char[])data</p> </blockquote> <p>is not required by the standard. Translated: A string implementation need not use this particular layout of its data. And it may have additional data.</p> <p>Now, your lightweight string class is frought with errors:</p> <pre><code>class pString { public: char c; // typically this is implementation detail, should be private pString * pString::operator=(const char *); // need ctors, dtors at least as well // won't you need any functions on strings? }; </code></pre> <p>Try something along the lines of the following:</p> <pre><code>/* a light-weight string class */ class lwstring { public: lwstring(); // default ctor lwstring(lwstring const&amp;); // copy ctor lwstring(char const*); // consume C strings as well lwstring&amp; operator=(lwstring const&amp;); // assignment ~lwstring(); // dtor size_t length() const; // string length bool empty() const; // empty string? private: char *_myBuf; size_t _mySize; }; </code></pre>
 

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