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

Pages: 1-

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 19:21 ID:zLom+jwj

the book probably meant "Such a default constructor does not initialize the object's data members".
In c++ data members may be objects of another class, or may be structs or builtin types, or may be pointers to objects. I think the book is saying that the default constructor is called for those data types are objects (not structs nor builtin types nor pointers). I'm not sure though, I haven't touched C++ in ages. I suggest picking up The C++ Programming Language, it's on pdf, just look for it.

Name: Anonymous 2007-07-21 20:47 ID:Heaven

Um, what's their not to get? It's just like the passage says, integral types are not initialized, object (aggregate) types will get their default ctor called. I suspect the OP is a troll. Proceed with caution.

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

I'm not a troll. I've only been doing this for about 3 weeks. Hence the title Beginner in C++.

Name: Anonymous 2007-07-21 22:01 ID:zLom+jwj

>>3
is not as simple as that.
ermm, re-reading The C++ Programming Language, I noticed that structs and classes are equivalent: my mistake. Let me copypaste a bit:

For example:
struct Tables {
int i;
int vi[10] ;
Table t1;
Table vt[10] ;
};
Tables tt;

Here, tt will be initialized using a generated default constructor that calls Table(15) for tt.t1 and
each element of tt.vt. On the other hand, tt.i and the elements of tt.vi are not initialized because
those objects are not of a class type. The reasons for the dissimilar treatment of classes and builtin
types are C compatibility and fear of runtime
overhead.
Because consts and references must be initialized (§5.5, §5.4), a class containing const or reference
members cannot be defaultconstructed
unless the programmer explicitly supplies a constructor
(§10.4.6.1). For example:
struct X {
const int a;
const int& r;
};
X x; / / error: no default constructor for X
Default constructors can be invoked explicitly (§10.4.10). Builtin
types also have default constructors
(§6.2.8).
~~~
Constructors [expr.ctor]
The construction of a value of type T from a value e can be expressed by the functional notation
T(e). For example:
void f(double d)
{
int i = int(d) ; / / truncate d
complex z = complex(d) ; / / make a complex from d
/ / ...
}
The T(e) construct is sometimes referred to as a functionstyle
cast. For a builtin
type T, T(e) is
equivalent to static_  cast<T>(e). Unfortunately, this implies that the use of T(e) is not always
safe. For arithmetic types, values can be truncated and even explicit conversion of a longer integer
type to a shorter (such as long to char) can result in undefined behavior. I try to use the notation
exclusively where the construction of a value is welldefined;
that is, for narrowing arithmetic conversions
(§C.6), for conversion from integers to enumerations (§4.8), and the construction of
objects of userdefined
types (§2.5.2, §10.2.3).
Pointer conversions cannot be expressed directly using the T(e) notation. For example,
char*(2) is a syntax error. Unfortunately, the protection that the constructor notation provides
against such dangerous conversions can be circumvented by using typedef names (§4.9.7) for
pointer types.
The constructor notation T() is used to express the default value of type T. For example:
void f(double d)
{
int j = int() ; / / default int value
complex z = complex() ; / / default complex value
/ / ...
}
The value of an explicit use of the constructor for a builtin
type is 0 converted to that type (§4.9.5).
Thus, int() is another way of writing 0. For a userdefined
type T, T() is defined by the default
constructor (§10.4.2), if any.
The use of the constructor notation for builtin
types is particularly important when writing templates.
Then, the programmer does not know whether a template parameter will refer to a builtin
type or a userdefined
type (§16.3.4, §17.4.1.2).
~~~~

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? ;-)

Name: Anonymous 2007-07-22 1:51 ID:3w9s6Dua

>>6
And where did you get a machine readable copy of TC++PL? ;-)
Dude, torrents.

Name: Anonymous 2007-07-22 2:06 ID:Heaven

go away

Name: Anonymous 2010-12-09 0:15

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