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: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).
~~~~

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