By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Default constructor construction

P: n/a
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.
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Satish Sangapu" <sk*****@hotmail.com> wrote...
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

Jul 22 '05 #2

P: n/a
"Satish Sangapu" <sk*****@hotmail.com> wrote in message news:<bp**********@news.lsil.com>...
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
Jul 22 '05 #3

P: n/a

"Icosahedron" <ga************@hotmail.com> wrote in message news:91************************@posting.google.com ...
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.
Jul 22 '05 #4

P: n/a
Satish Sangapu wrote:
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.

Jul 22 '05 #5

P: n/a
Satish Sangapu wrote:
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.
The book was written in 1996 - I wonder if stuff like this has changed by
now?

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.