How do C++ class members get initialized if I don’t do it explicitly?

Suppose I have a class with private memebers ptr, name, pname, rname, crname and age. What happens if I don’t initialize them myself? Here is an example:

class Example {
    private:
        int *ptr;
        string name;
        string *pname;
        string &rname;
        const string &crname;
        int age;

    public:
        Example() {}
};

And then I do:

int main() {
    Example ex;
}

How are the members initialized in ex? What happens with pointers? Do string and int get 0-intialized with default constructors string() and int()? What about the reference member? Also what about const references?

I’d like to learn it so I can write better (bug free) programs. Any feedback would help!

8 Answers
8

Leave a Comment