📄 string.constructors.html
字号:
<html><!-- #BeginTemplate "/Templates/tmpl.dwt" --><head><!-- #BeginEditable "doctitle" --> <title>PTypes: string: constructors/destructors</title><!-- #EndEditable --> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"><link rel="stylesheet" href="styles.css"></head><body bgcolor="#FFFFFF" leftmargin="40" marginwidth="40"><p><a href="../index.html"><img src="title-1.7.gif" width="213" height="34" alt="C++ Portable Types Library (PTypes) Version 1.7" border="0"></a> <hr noshade><!-- #BeginEditable "body" --> <p class="hpath"><a href="index.html">Top</a>: <a href="basic.html">Basic types</a>: <a href="string.html">string</a>: Constructors/destructors</p><blockquote> <pre class="lang">#include <ptypes.h>string::string();string::string(const string&);string::string(char);string::string(const char*);string::string(const char*, int);string::~string();</pre></blockquote><p>A string object can be constructed in 5 different ways:</p><ul><li> <p> default constructor <span class="lang">string::string()</span> creates an empty string.</p></li><li> <p>copy constructor <span class="lang">string::string(const string& s)</span> creates a copy of the given string <span class="lang">s</span>. Actually this constructor only increments the reference count by 1 and no memory allocation takes place.</p></li><li> <p><span class="def">string::string(char c)</span> constructs a new string consisting of one character <span class="lang">c</span>.</p></li><li> <p><span class="def">string::string(const char* s)</span> constructs a new string object from a null-terminated string. If <span class="lang">s</span> is either <span class="lang">NULL</span> or is a pointer to a null character, an empty string object is created. This constructor can be used to assign a string literal to a string object (see examples below).</p></li><li> <p><span class="def">string::string(const char* s, int len)</span> copies <span class="lang">len</span> bytes of data from buffer <span class="lang">s</span> to the newly allocated string buffer.</p></li></ul><p>Destructor <span class="lang">~string()</span> decrements the reference count for the given string buffer and removes it from the dynamic memory if necessary.</p><p><b>Examples:</b></p><blockquote> <pre>string S1; <span class="comment">// empty string</span>string S2 = S1; <span class="comment">// copy</span>string S3 = 'A'; <span class="comment">// single character</span>string S4 = "ABCabc"; <span class="comment">// string literal</span>char* p = "ABCabc";string S5 = p; <span class="comment">// null-terminated string</span>string S6(p, 3); <span class="comment">// buffer/length</span></pre></blockquote><p class="seealso">See also: <a href="string.operators.html">Operators</a>, <a href="string.typecasts.html">Typecasts</a>, <a href="string.manipulation.html">Manipulation</a></p><!-- #EndEditable --><hr size="1"><a href="../index.html" class="ns">PTypes home</a></body><!-- #EndTemplate --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -