Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ empty String constructor

Tags:

c++

string

I am a C++ beginner, so sorry if the question is too basic.

I have tried to collect the string constrcturs and try all them out (to remember them).

string strA();          // string(); empty string // incorrect
string strB("Hello");   // string( const char* str)
string strC("Hello",3); // string( const char* str, size_type length)
string strD(2,'c');     // string( size_type lenght, const char &c)
string strE(strB);      // string( const string& s)

cout << strA << endl;
cout << strB << endl;
cout << strC << endl;
cout << strD << endl;
cout << strE << endl;

All of them works except for the strA. It prints "1". Why? Whats the type of the strA in this case? How can I check the type of stuff when I am unsure?

I have noticed that the correct way is this (which by the way seems to be inconsistent with the other constructors, sometimes parens sometimes no parens):

string strA;

ps: question in bold, usual irrelevant answers will be downvoted.

like image 673
George Avatar asked May 29 '09 11:05

George


People also ask

How do you declare an empty string?

In C#, you can use strings as an array of characters, However, more common practice is to use the string keyword to declare a string variable. The string keyword is an alias for the System. String class. To declare an empty string.

Can you have an empty string in C?

In C language strings are terminated with a null character \0 , so we can check if a given string is empty or not by verifying if a string contains the first character is \0 . Note: C language doesn't have any built-in function to check if a string is empty, like we have a empty() function in C++.

How do you initialize std::string to empty string?

The closest you can get is to check whether the string is empty or not, using the std::string::empty method.. The default construction of std::string is not "" ; it is {} . Default-constructing avoids having to check the char const* , find it is an empty string, and eventually do nothing.

Is std::string empty?

std::string::emptyReturns whether the string is empty (i.e. whether its length is 0). This function does not modify the value of the string in any way.


1 Answers

This is a very popular gotcha. C++ grammar is ambiguous. One of the rules to resolve ambiguities is "if something looks like declaration it is a declaration". In this case instead of defining a variable you declared a function prototype.

string strA();

is equivalent to

string strA(void);

a prototype of a no-arg function which returns string.

If you wish to explicitly call no-arg constructor try this:

string strA=string();

It isn't fully equivalent - it means 'create a temporary string using no-arg constructor and then copy it to initialize variable strA', but the compiler is allowed to optimize it and omit copying.

EDIT: Here is an appropriate item in C++ FAQ Lite

like image 195
Tadeusz Kopec for Ukraine Avatar answered Oct 10 '22 15:10

Tadeusz Kopec for Ukraine