Default constructor construction

S

Satish Sangapu

I am reading "Inside the C++ Object Model" by Stanley Lippman. It is a very
nice book about the internals of C++.
Anyway, I am confused about one thing. He states that a default constructor
is not ALWAYS generated by the compiler - ONLY when it's NEEDED and then he
goes on to define NEEDED.

class one
{
private:
int a;
bool b;
};

Apparently, the above class won't have a default constructor generated by
the compiler.

But,

class data
{
public:
data();
};

class two
{
private:
data d;
};

The above two class will have a default constructor generated by the
compiler.

The book was written in 1996 - I wonder if stuff like this has changed by
now?

Thanks.
 
V

Victor Bazarov

Satish Sangapu said:
I am reading "Inside the C++ Object Model" by Stanley Lippman. It is a very
nice book about the internals of C++.
Anyway, I am confused about one thing. He states that a default constructor
is not ALWAYS generated by the compiler - ONLY when it's NEEDED and then he
goes on to define NEEDED.

class one
{
private:
int a;
bool b;
};

Apparently, the above class won't have a default constructor generated by
the compiler.

But,

class data
{
public:
data();
};

class two
{
private:
data d;
};

The above two class will have a default constructor generated by the
compiler.

The book was written in 1996 - I wonder if stuff like this has changed by
now?

The default constructor by the definition can be _trivial_ or
_non-trivial_. In case of "class one", it's _trivial_. For
the "class two", the only member has non-trivial constructor,
which makes "class two"s constructor also non-trivial.

See section 12.1 of the Standard.

Victor
 
I

Icosahedron

Satish Sangapu said:
I am reading "Inside the C++ Object Model" by Stanley Lippman. It is a very
nice book about the internals of C++.
Anyway, I am confused about one thing. He states that a default constructor
is not ALWAYS generated by the compiler - ONLY when it's NEEDED and then he
goes on to define NEEDED.

class one
{
private:
int a;
bool b;
};

Apparently, the above class won't have a default constructor generated by
the compiler.

But,

class data
{
public:
data();
};

class two
{
private:
data d;
};

The above two class will have a default constructor generated by the
compiler.

The book was written in 1996 - I wonder if stuff like this has changed by
now?

Thanks.

While perhaps it has changed, I doubt it would still generate a
default constructor. The first class has no user defined types, so it
doesn't need a constructor. If you declare an int or float or
something else on the stack in a function, and don't initialize it,
you get garbage because there is no constructor for POD
(plain-old-data) types.

class two, because it has a member of class data, which has a
constructor, generates a constructor to do one thing, call the
constructor of data.

HTH,

Jay
 
R

Ron Natalie

Icosahedron said:
you get garbage because there is no constructor for POD
(plain-old-data) types.

Any class that doesn't provide a declaration of a constructor, gets a
default constructor impliictly declared by the compiler. POD's are not an exception to this
rule (as a matter of fact, the rule primarily exists to support POD's).

A constructor is defined whenever it is needed, that is, when someone
actually tries to build a default-constructed object. Most compilers will
optimize away the call of a trivial cosntructor, but that doesn't change
it's "existance" as far as the language is concerned.
 
E

E. Robert Tisdale

Satish said:
I am reading "Inside the C++ Object Model" by Stanley Lippman.
It is a very nice book about the internals of C++.
Anyway, I am confused about one thing. He states that
a default constructor is not ALWAYS generated by the compiler -
ONLY when it's NEEDED and then he goes on to define NEEDED.

class one {
private:
int a;
bool b;
};

Apparently, the above class won't have
a default constructor generated by the compiler.
Wrong.

But,

class data {
public:
data(void);
};

class two {
private:
data d;
};

The above two class will have
a default constructor generated by the compiler.

Wrong.
The default constructor for class data
*must* be provided by you -- the programmer.
The book was written in 1996.
I wonder if stuff like this has changed by now?

Nothing has changed in this regard.
C++ compilers will "provide" a default constructor,
a copy constructor and an assignment operator
if you don't declare them.

You need to be careful about what you mean by
"generated by the compiler".
C++ compilers don't need to write a default constructor.
All they really need to do is emit code to construct the object.
There may be no code that you can identify as
"a call to the default constructor". It might be "as if"
you defined all of your constructors inline
and all of the code was "optimized away".
The notion that the compiler "provided" or "generated"
a default constructor may be purely conceptual.
 
A

Andrey Tarasevich

Satish said:
I am reading "Inside the C++ Object Model" by Stanley Lippman. It is a very
nice book about the internals of C++.
Anyway, I am confused about one thing. He states that a default constructor
is not ALWAYS generated by the compiler - ONLY when it's NEEDED and then he
goes on to define NEEDED.

"Generated" is not a precise term in this context. Perhaps this term was
explained in the book, but since you didn't provide the explanation here
there is no way to know what it means. The C++ specification uses terms
such terms as "declare" and "define" when it comes to providing implicit
constructors.
class one
{
private:
int a;
bool b;
};

Apparently, the above class won't have a default constructor generated by
the compiler.

This class will have a default constructor implicitly _declared_ by the
compiler. However, the compiler will not try to _define_ it unless it is
really necessary (i.e. unless it is used in the program).
But,

class data
{
public:
data();
};

class two
{
private:
data d;
};

The above two class will have a default constructor generated by the
compiler.

No. Since you declared default constructor for class 'data' explicitly,
the compiler will not define it for you. You have to do it yourself.

As for the class 'two', the situation here is the same as with class
'one' - the compiler will declare the constructior and will make an
attempt to define it, if it is needed in the program.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,834
Members
47,382
Latest member
MichaleStr

Latest Threads

Top