Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Beginner in C++, Really need help.

Name: Anonymous 2007-07-21 19:07 ID:KiegSQe4

I'm currently in my first C++ class (on chapter 3 of the book), and this is a question that I have to answer.

How are an object’s data members initialized if a class has only an implicitly defined default constructor?

The passage that I'm reviewing in the book says:
The compiler implicitly creates a default constructor in a class that does not define a constructor. Such a default constructor does not initialize the class's data members, but does call the default constructor for each data member that is an object of another class.

The first answer I thought of was the object's data members aren't initialized, but the book says class's data members and not object's. And I'm not fully understanding what the second part from the book means.

I was hoping for some clarification and guidance.

Name: Anonymous 2007-07-21 22:13 ID:CLQNAIY1

Looks like someone beat me to it but in other words a data member of a built-in type will be uninitialized (set to garbage data) by the automatically generated default constructor, but data members of user defined types will have their default ctors (constructors) called.

Note that structs aren't built-in types, like >>5 says.  In C++ structs *are* classes, it is just a different keyword.  The only difference is that the members of a struct are public by default whereas the members of a class are private by default (i.e., if you forget to explicitly say public: or private:).

Another quirky thing: 'default constructor' has a weird meaning.  It is any constructor that can be called with no arguments.  Therefore something like MyType(int x = 0) {} is a default ctor.    Also, the compiler automatically generates a default ctor if and only if you have defined no constructors;  If you define a non-default constructor the compiler will not generate a default one for you.

And where did you get a machine readable copy of TC++PL? ;-)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List