Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ -- which constructor of string is call when we use string(0)?

Tags:

c++

Given the following code,

class A
{
public:
  A() : str(0) {}
private:
  string str;
};

Based on this http://www.cplusplus.com/reference/string/string/string/

string ( );
string ( const string& str );
string ( const string& str, size_t pos, size_t n = npos );
string ( const char * s, size_t n );
string ( const char * s );
string ( size_t n, char c );
template<class InputIterator> string (InputIterator begin, InputIterator end);

I don't see which constructor of string is called when we use the 'str(0)'.

Question> Can someone tell me which string constructor is used for 'str(0)'?

like image 812
q0987 Avatar asked Dec 06 '25 04:12

q0987


1 Answers

This one:

string ( const char * s );

It's converted to a null pointer. (And also gives you undefined behavior, since s cannot be a null pointer.)

like image 193
GManNickG Avatar answered Dec 08 '25 17:12

GManNickG