Search code examples
c++stringinitializationprivateaccess-modifiers

Does a class string always gets initialized to empty?


I have this string s and t in class.

class A
{
    private:
    static string s;
    string t;
    public:
    void printString()
    {
        cout<<s<<t<<endl;
    }
};

I tried printing these string and everytime they were empty if not initialized. So my question is whether a class string is always empty before initialization by a constructor. Also is it true for both static and non static strings?


Solution

  • In both cases, the default string constructor will be called. The default constructor for the std::string class will always initialize it to an empty string.