0

Different between two codes

What is different between: std::string a; a = "Hello, world!" and: std::string a = "Hello, world"

25th Aug 2019, 6:55 AM
MineFuf LP
MineFuf LP - avatar
1 Answer
+ 5
In the first example, you are creating a string object, and the default constructor of the string class is called. This initializes the string with an empty string literal, "". You then assign a new string literal to the string object. In the second example, an alternate constructor of the string class is called. The string object is initialized directly with a string literal which is provided by you. In normal cases, there is no reason to do as in the first example if you plan on initializing it directly on the second line. For further reading, I suggest looking at std::string and its different constructors. http://www.cplusplus.com/reference/string/string/string/
25th Aug 2019, 7:01 AM
Hatsy Rei
Hatsy Rei - avatar