0

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?

  • 1
    It depends on what you mean by "before being initialized by a constructor". Do you mean "before being initialized by an `std::string` constructor" or "before being initialized by `A`'s constructor". – François Andrieux Nov 26 '20 at 17:04
  • Yeah I meant A's constructor, so does that mean `std::string s` is always initialised to empty? –  Nov 26 '20 at 17:05
  • 2
    unless you specify using an alternate constructor, yes, the default one will initialize the `std::string` to empty. [See implementation (1) here](https://en.cppreference.com/w/cpp/string/basic_string/basic_string). – WhozCraig Nov 26 '20 at 17:06
  • @pujadeo `s` is initialized before `main` but may be uninitialized in some contexts, such as during the initialization of other `static` members from other `.cpp` files. How `s` is initialized depends on how it was defined in the source file. There should be a `std::string A::s;` statement or equivalent in a `.cpp` file which determines how it is constructed. – François Andrieux Nov 26 '20 at 17:07
  • `specify using an alternate constructor` do you mean alternate constructor for `std::string` ? How do we do that, could you point me to some link please? –  Nov 26 '20 at 17:08
  • The questions in your comments are starting to deviate from the question body. Please edit the question itself if you want to ask something different. – cigien Nov 26 '20 at 17:10
  • @cigien bother to explain how? If I am asking WhozCraig to explain what he just said –  Nov 26 '20 at 17:11
  • Ah, I see. I thought you were trying to ask a different question. Comments are fine, but note that the comment section is not really meant for extended discussions. – cigien Nov 26 '20 at 17:12

1 Answers1

1

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Alex M
  • 26
  • 2
  • 1
    The part that asks "before initialization by a constructor" may invalidate this answer depending on what it means. You can call `printString()` before `s`'s constructor is called. – François Andrieux Nov 26 '20 at 17:17
  • @FrançoisAndrieux Wouldn't that be UB? – cigien Nov 26 '20 at 17:20
  • @cigien Yes, but it contradicts the implication that `s` will definitely always be empty. It may be indeterminate. – François Andrieux Nov 26 '20 at 17:22
  • @FrançoisAndrieux That's true. I suppose the answer could clarify that, but I don't think that part is too necessary. – cigien Nov 26 '20 at 17:23
  • @FrançoisAndrieux How can printString() be called before s's constructor is called. Because everytime an object is created the first thing that ll happen is the variables would get space in memory and their respective ctor will be called –  Nov 26 '20 at 17:28
  • 1
    @Equinox One example is if the constructor of a `static` member or global object from another source file ends up calling `A::printString()`. The object `s` may or may not have been constructed yet. – François Andrieux Nov 26 '20 at 17:32
  • But exactly, how is that possible when `printString` itself is not static which means cannot be accessible as `A::printString()` how can we access it without creating object. Wouldn't that give error. –  Nov 26 '20 at 17:37
  • @Equinox Consider a static member or global `B` type object where `B` has an `A` member, or where `B`'s constructor needs an `A` to do some work for its own initialization. You can have an `A` before `A::s` is initialized. – François Andrieux Nov 26 '20 at 18:00