473,386 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Default constructor construction

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
5 1907
"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
"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

"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Ook | last post by:
I'm having trouble comprehending what exactly "default construction" is. I know how to provide a constructor with initial values, so that if I, for example, in my code do this: MyClass...
7
by: shikn | last post by:
I wrote one simple piece of codes to test const objects in C++ as below: ================================= class Empty{}; main() { const Empty e1; } ==================================
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.