If a string is defined like this
std::string name;
What will be the value of the uninitialized string "name" and what size it would be?
Because it is not initialized, it is the default constructor that is called. Then : empty string constructor (default constructor) : Constructs an empty string, with a length of zero characters.
Strings. Variables of type "String" are treated as Objects in this case and have a default value of null.
A std::string isn't a pointer, so it shouldn't be combined. PS. the initializations are the same: the ctor of std::string sets it to the empty string.
The std::string class manages the underlying storage for you, storing your strings in a contiguous manner. You can get access to this underlying buffer using the c_str() member function, which will return a pointer to null-terminated char array. This allows std::string to interoperate with C-string APIs.
Because it is not initialized, it is the default constructor that is called. Then :
empty string constructor (default constructor) :
Constructs an empty string, with a length of zero characters.
Take a look : http://www.cplusplus.com/reference/string/string/string/
EDIT : As stated in C++11, §21.4.2/1 :
Effects: Constructs an object of class basic_string. The postconditions of this function are indicated in Table 63.
-> Table 63 +-----------------------------------------------------------------------------+ | data() | a non-null pointer that is copyable and can have 0 added to it | +------------+----------------------------------------------------------------+ | size() | 0 | +------------+----------------------------------------------------------------+ | capacity() | an unspecified value | +-----------------------------------------------------------------------------+
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With