Search code examples
c++class-constructors

implicit constructors for String class


String s2("hi");

Do I have to write an implicit string constructor

String::String(const char* str);

or

would this constructor handle it:

String::String(const String &str);


Solution

  • When you create this string object, string (const char* s) constructor will be called, so there is a no need of writing a constructor.

    Here are the constructors which are defined inside the std::string class

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

    Further don't try to edit some standard libraries, It will lead to unwanted issues. if you want any customized functions, write a wrapper of your own.