473,378 Members | 1,378 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,378 software developers and data experts.

About member variable initilization and default constructor issues

In Inside C++ object Model, Lippman said there are four cases in which
compile will sythesize a default constructor to initialize the member
variables if the constructor is absent:

1. there is a virtual function;

2. virtual inheritance;

3.base class with explicit default constructor;

4.member object with explicit default constructor.

e.g.

class A {

public:

int i;

A* p;
}
int main()
{
A a;
if(a.i ==0 || a.p == 0) //do something
}

The behavior is undefined for the above case. If we modify class A to
have any one of the four characeristics, then the member variables
will be initialized in the default constructor sythesysized by the
compiler, as though
A():i(0),p(0){} is defined in the class.

But I try this in different compilers, and yield different results. My
question is : Is Lippman teaching ISO standard, or compiler-
dependent? Thanks

Oct 30 '07 #1
10 2384
JosephLee wrote:
In Inside C++ object Model, Lippman said there are four cases in which
compile will sythesize a default constructor to initialize the member
variables if the constructor is absent:

1. there is a virtual function;

2. virtual inheritance;

3.base class with explicit default constructor;

4.member object with explicit default constructor.

e.g.

class A {

public:

int i;

A* p;
}
int main()
{
A a;
if(a.i ==0 || a.p == 0) //do something
}

The behavior is undefined for the above case. If we modify class A to
have any one of the four characeristics, then the member variables
will be initialized in the default constructor sythesysized by the
compiler, as though
A():i(0),p(0){} is defined in the class.

But I try this in different compilers, and yield different results. My
question is : Is Lippman teaching ISO standard, or compiler-
dependent? Thanks
The Standard says that the constructor is trivial if it's implicitly
defined and the class no virtual functions or virtual bases, all direct
base classes have trivial c-tors, all non-static data members also have
trivial c-tors. So, turn that around and you get what Lippman says.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 30 '07 #2
Hi

Victor Bazarov wrote:
JosephLee wrote:
>In Inside C++ object Model, Lippman said there are four cases in which
compile will sythesize a default constructor to initialize the member
variables if the constructor is absent:

1. there is a virtual function;

2. virtual inheritance;

3.base class with explicit default constructor;

4.member object with explicit default constructor.
[...]
>If we modify class A to
have any one of the four characeristics, then the member variables
will be initialized in the default constructor sythesysized by the
compiler, as though
A():i(0),p(0){} is defined in the class.

The Standard says that the constructor is trivial if it's implicitly
defined and the class no virtual functions or virtual bases, all direct
base classes have trivial c-tors, all non-static data members also have
trivial c-tors. So, turn that around and you get what Lippman says.
I disagree. Firstly, it's not correct that if 1-4 is not satisfied then
there will be no default-constructor synthesized:

"If there is no user-declared constructor for class X, a default constructor
is implicitly declared."

"An implicitly-declared default constructor for a class is implicitly
defined when it is used to create an object of its class type. The
implicitly-defined default constructor performs the set of initializations
of the class that would be performed by a user-written default constructor
for that class with an empty mem-initializer-list and an empty function
body"

Secondly, the latter phrase explains that in both of the above cases, the
program will behave as if there was a user-defined constructor

A() {}

This means that neither i nor p will be initialized.

Markus

Oct 30 '07 #3
Markus Moll wrote:
Hi

Victor Bazarov wrote:
>JosephLee wrote:
>>In Inside C++ object Model, Lippman said there are four cases in
which compile will sythesize a default constructor to initialize
the member variables if the constructor is absent:

1. there is a virtual function;

2. virtual inheritance;

3.base class with explicit default constructor;

4.member object with explicit default constructor.
[...]
>>If we modify class A to
have any one of the four characeristics, then the member variables
will be initialized in the default constructor sythesysized by the
compiler, as though
A():i(0),p(0){} is defined in the class.

The Standard says that the constructor is trivial if it's implicitly
defined and the class no virtual functions or virtual bases, all
direct base classes have trivial c-tors, all non-static data members
also have trivial c-tors. So, turn that around and you get what
Lippman says.

I disagree. Firstly, it's not correct that if 1-4 is not satisfied
then there will be no default-constructor synthesized:

"If there is no user-declared constructor for class X, a default
constructor is implicitly declared."

"An implicitly-declared default constructor for a class is implicitly
defined when it is used to create an object of its class type. The
implicitly-defined default constructor performs the set of
initializations of the class that would be performed by a
user-written default constructor for that class with an empty
mem-initializer-list and an empty function body"

Secondly, the latter phrase explains that in both of the above cases,
the program will behave as if there was a user-defined constructor

A() {}

This means that neither i nor p will be initialized.
Right. I am not sure with whom you're arguing. The four specific
traits that cause an implicitly defined non-trivial c-tor in the
class are the same as in the Standard. Lippman says that the c-tor
is generated if any of the four is present. The Stadard says that
the c-tor is trivial if none of the four is present. In that sense
Lippman teaches the Standard. No?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 30 '07 #4
Hi

Victor Bazarov wrote:
Right. I am not sure with whom you're arguing. The four specific
traits that cause an implicitly defined non-trivial c-tor in the
class are the same as in the Standard. Lippman says that the c-tor
is generated if any of the four is present. The Stadard says that
the c-tor is trivial if none of the four is present. In that sense
Lippman teaches the Standard. No?
1. I fail to see where it says that to say a constructor is trivial is the
same as to say that it isn't generated.
2. I disagree with the original claim that e.g. for a class like:

struct A
{
virtual void dummy() {} // virtual function present
int i;
A *p;
};

the compiler would generate a constructor as if A() : i(0), p(0) {}

Okay. 1 doesn't make any difference, because it wouldn't be observable
anyway. But 2 is simply wrong. Or am I?

Markus

Oct 30 '07 #5
Markus Moll wrote:
Hi

Victor Bazarov wrote:
>Right. I am not sure with whom you're arguing. The four specific
traits that cause an implicitly defined non-trivial c-tor in the
class are the same as in the Standard. Lippman says that the c-tor
is generated if any of the four is present. The Stadard says that
the c-tor is trivial if none of the four is present. In that sense
Lippman teaches the Standard. No?

1. I fail to see where it says that to say a constructor is trivial
is the same as to say that it isn't generated.
Where *what* says that? The standard? The book?

An implicitly declared default c-tor is implicitly defined for a class.
There seems to be no clear connection between the terms "non-trivial"
and "implicitly defined". "Trivial" is used to define the lifetime of
objects and the behaviour of 'delete', for example. "Implicitly
defined" sets forth certain relation between the type and its bases and
members (bases should have their implicitly declared c-tors implicitly
defined if this class has its c-tor implicitly defined).

However, it's every author's right to draw parallels or make guesses as
to what the Standard actually means. Considering that Lippman is one
of the authorities in C++, he probably knows what he's talking about if
from his text it seems apparent that "non-trivial" and "implicitly
defined" and "generated" all mean basically the same thing.
2. I disagree with the original claim that e.g. for a class like:

struct A
{
virtual void dummy() {} // virtual function present
int i;
A *p;
};

the compiler would generate a constructor as if A() : i(0), p(0) {}
OK. I thought you disagreed with something I posted since you replied
to my post and not to the one you disagreed with.
Okay. 1 doesn't make any difference, because it wouldn't be observable
anyway. But 2 is simply wrong. Or am I?
As I can see, you quoted part of the Standard that explains that it is
incorrect to expect zero-initialisation of POD members by the implicitly
defined constructor. I agree.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 30 '07 #6
Hi

Victor Bazarov wrote:
Markus Moll wrote:
>1. I fail to see where it says that to say a constructor is trivial
is the same as to say that it isn't generated.

Where *what* says that? The standard? The book?
The standard, preferably.

Sorry... there seems to be more confusion than I thought :-)

First: I have no idea about the book. I only know what JosephLee posted.
His original posting reads (excerpt):
In Inside C++ object Model, Lippman said there are four cases in which
compile will sythesize a default constructor to initialize the member
variables if the constructor is absent:
[Cases follow]

You replied:
The Standard says that the constructor is trivial if [...]
*So, turn that around and you get what Lippman says.
I had the impression that you were talking about different things. JosephLee
about when the default constructor would be provided by the compiler, you
about when it would be trivial.

Furthermore, JosephLee originally wrote:
If we modify class A to have any one of the four characeristics, then the
member variables will be initialized in the default constructor
sythesysized by the compiler, as though A():i(0),p(0){} is defined in the
class.
Which I still think is wrong, and apparently you agree with my reasoning
(see below). That was everything I wanted to express.
However, it's every author's right to draw parallels or make guesses as
to what the Standard actually means.
Sure. As I said, I don't know anything about the book, so I can't say what
parallels the author draws.
Considering that Lippman is one of the authorities in C++, he probably
knows what he's talking about if from his text it seems apparent
that "non-trivial" and "implicitly defined" and "generated" all mean
basically the same thing.
Huh? Proof by authority? That comes right after proof by assertion. ;-)
OK. I thought you disagreed with something I posted since you replied
to my post and not to the one you disagreed with.
Well... I disagreed with your agreeing with the original post... oh...
well...
As I can see, you quoted part of the Standard that explains that it is
incorrect to expect zero-initialisation of POD members by the implicitly
defined constructor. I agree.
Good. But you seemed to also agree when JosephLee stated the opposite.

Sorry, I'm really starting to get confused...

Markus

Oct 30 '07 #7
On Oct 30, 4:49 pm, JosephLee <shoupe...@gmail.comwrote:
In Inside C++ object Model, Lippman said there are four cases in which
compile will sythesize a default constructor to initialize the member
variables if the constructor is absent:
Note that Lippman is talking about the internals of a C++
compiler, not about the language specification. According to
the language specification, a default constructor is always
synthesized if there is no user defined constructor. The
conditions below define what the standard calls a "trivial"
constructor (or rather the inverse); if the constructor is
"trivial", it's a no-op, and internally, the compiler doesn't do
anything.
1. there is a virtual function;
2. virtual inheritance;
3.base class with explicit default constructor;
4.member object with explicit default constructor.
That should be member object or base class with non-trivial
constructor.

And of course, the compiler doesn't even consider synthesizing a
default constructor if you have declared any other constructor.
e.g.
class A {
public:
int i;
A* p;
}
int main()
{
A a;
if(a.i ==0 || a.p == 0) //do something
}
The behavior is undefined for the above case.
Yes.
If we modify class A to
have any one of the four characeristics, then the member variables
will be initialized in the default constructor sythesysized by the
compiler, as though
A():i(0),p(0){} is defined in the class.
No. The synthesized constructor doesn't initialize int's or
pointers.
But I try this in different compilers, and yield different
results. My question is : Is Lippman teaching ISO standard, or
compiler-dependent?
Well, he's talking about compiler internals, so it's definitely
implementation specific. But I'm rather surprised that he says
that the synthesized constructor will initialize the variables
above; the standard doesn't require it, and none of the
compilers on which Lippman has worked do it.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 31 '07 #8
On Oct 30, 7:28 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Markus Moll wrote:
Victor Bazarov wrote:
Right. I am not sure with whom you're arguing. The four specific
traits that cause an implicitly defined non-trivial c-tor in the
class are the same as in the Standard. Lippman says that the c-tor
is generated if any of the four is present. The Stadard says that
the c-tor is trivial if none of the four is present. In that sense
Lippman teaches the Standard. No?
1. I fail to see where it says that to say a constructor is trivial
is the same as to say that it isn't generated.
Note that the word in the book is "synthesized", not
"generated". But I think the difference is more one of context;
the book is talking about what the compiler does: in the case of
a trivial constructor, the compiler doesn't do anything. From a
language point of view, there is a constructor, but it doesn't
result in any code, which is what Lippman is talking about.
Where *what* says that? The standard? The book?
An implicitly declared default c-tor is implicitly defined for
a class. There seems to be no clear connection between the
terms "non-trivial" and "implicitly defined".
It seems very clear to me: if the class meets certain criteria,
the implicitly defined constructor is trivial. By definition,
no explicitly defined constructor (even one with an empty inline
body) is trivial. The set of all trivial constructors is a
subset of the set of all implicitly defined constructors.
"Trivial" is used to define the lifetime of
objects and the behaviour of 'delete', for example.
Trivial has nothing to do with the lifetime of objects. It is a
categoristic of default constructors, copy constructors, copy
assignment operators and destructors. Whether are trivial
authorizes some other operations: they all must be trivial, for
example, if the type is to be a member of a union.

Triviality is relevant to lifetime only in so far that if the
constructor is trivial, lifetime of the object is considered to
start as soon as the memory for it is allocated, and if the
destructor is trivial, lifetime of the object is considered to
last until the memory is freed or reused.
"Implicitly defined" sets forth certain relation between the
type and its bases and members (bases should have their
implicitly declared c-tors implicitly defined if this class
has its c-tor implicitly defined).
I don't think so. A special member function is implicitly
defined if it is implicitly declared and is used. It is
implicitly declared if the user hasn't explicitly declared it,
and in the case of a default constructor, if the user hasn't
explicitly declared any other constructor.
However, it's every author's right to draw parallels or make
guesses as to what the Standard actually means. Considering
that Lippman is one of the authorities in C++, he probably
knows what he's talking about if from his text it seems
apparent that "non-trivial" and "implicitly defined" and
"generated" all mean basically the same thing.
Actually, the quoted text is a lot more precise than that. It
speaks of when the compiler "synthesizes" a default constructor.
The standard never uses the word "synthesize", and from the name
of the book, I'm pretty sure the Lippman is talking about
compiler internals here---when does the compiler generate code
on its own for the constructor.

The answer is, of course, whenever the constructor is implicitly
declared, is used, and is not trivial. The definition of
"trivial", in the standard, is designed intentionally so that
the compiler implementation can do nothing in the case of a
trivial default constructor or a trivial destructor, and can do
a bitwise copy in the case of a trivial copy constructor or a
trivial assignment operator.
2. I disagree with the original claim that e.g. for a class like:
struct A
{
virtual void dummy() {} // virtual function present
int i;
A *p;
};
the compiler would generate a constructor as if A() : i(0), p(0) {}
I disagree with his disagreement. A compiler very well could
generate the constructor like this. It's not required, however,
and none that I use do.
OK. I thought you disagreed with something I posted since you
replied to my post and not to the one you disagreed with.
Okay. 1 doesn't make any difference, because it wouldn't be
observable anyway. But 2 is simply wrong. Or am I?
As I can see, you quoted part of the Standard that explains
that it is incorrect to expect zero-initialisation of POD
members by the implicitly defined constructor. I agree.
It is, in fact, undefined behavior if you even read the values
(except if you read them as a character type).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 31 '07 #9
On Oct 30, 11:45 pm, Markus Moll <m...@rbg.informatik.tu-darmstadt.de>
wrote:
Victor Bazarov wrote:
Markus Moll wrote:
1. I fail to see where it says that to say a constructor is trivial
is the same as to say that it isn't generated.
Where *what* says that? The standard? The book?
The standard, preferably.
Sorry... there seems to be more confusion than I thought :-)
First: I have no idea about the book. I only know what
JosephLee posted. His original posting reads (excerpt):
In Inside C++ object Model, Lippman said there are four
cases in which compile will sythesize a default constructor
to initialize the member variables if the constructor is
absent:
[Cases follow]
You replied:
The Standard says that the constructor is trivial if [...]
So, turn that around and you get what Lippman says.
I had the impression that you were talking about different things.
He is, in a way. Victor is talking about what the standard
says, using the terminology used in the standard. Lippman is
talking about what a compiler does, using a terminology which is
not that of the standard (possibly intentionally, to avoid the
risk of confusion). Two completely different things... except
that the authors of the standard weren't totally ignorant about
how compilers work---the wording in the standard concerning
"trivial" default constructors is intentially designed to
specify the case where the "generated" constructor is a no-op,
i.e. where the compiler doesn't need to "synthesize" anything.
JosephLee about when the default constructor would be provided
by the compiler, you about when it would be trivial.
No he didn't. JosephLee was also comparing different cases
where the compiler provided the constructor: he was comparing
the case where the constructor was trivial (the compiler didn't
have to synthesize anything) to when it wasn't (the compiler had
to generate code, even though you hadn't written any). The
question concerned the required behavior of the synthesized code
(and I'm pretty sure that JosephLee misunderstood what Lippman
wrote---Lippman was, along with Stroustrup, one of the lead
authors of CFront; I've used that compiler, and it doesn't
behave as JosephLee seems to think Lippman says it should).
Furthermore, JosephLee originally wrote:
If we modify class A to have any one of the four
characeristics, then the member variables will be
initialized in the default constructor sythesysized by the
compiler, as though A():i(0),p(0){} is defined in the class.
Which I still think is wrong, and apparently you agree with my
reasoning (see below).
It's wrong, we all agree that it's wrong, and I'm almost sure
that Lippman didn't write anything else.

[...]
Considering that Lippman is one of the authorities in C++,
he probably knows what he's talking about if from his text
it seems apparent that "non-trivial" and "implicitly
defined" and "generated" all mean basically the same thing.
Huh? Proof by authority? That comes right after proof by
assertion. ;-)
Yes and no. The fact that Lippman has actually written C++
compilers would suggest very strongly that he knows something
about C++, and that his discussions concerning compiler
internals aren't too far off base. To the point where I'd
assume that JosephLee has misunderstood him.

In context, of course, it also seems probable that he is using
the word "sythesysize" to refer to what the compiler does,
internally. If I understand correctly, that's the whole point
of his book: what does the compiler do with the source code you
feed it?
OK. I thought you disagreed with something I posted since
you replied to my post and not to the one you disagreed
with.
Well... I disagreed with your agreeing with the original
post... oh... well...
Victor didn't agree with the original post. He agreed with what
Lippman said.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 31 '07 #10
Hi

James Kanze wrote:
The question concerned the required behavior of the synthesized code
(and I'm pretty sure that JosephLee misunderstood what Lippman
wrote---Lippman was, along with Stroustrup, one of the lead
authors of CFront; I've used that compiler, and it doesn't
behave as JosephLee seems to think Lippman says it should).
Well, as I said. Neither do I really know who Lippman is nor have I read his
books. So I _couldn't possibly_ disagree with Lippman, but just with
JosephLee's statements/conclusions. ;-)
>Which I still think is wrong, and apparently you agree with my
reasoning (see below).

It's wrong, we all agree that it's wrong, and I'm almost sure
that Lippman didn't write anything else.
Again: I was only referring to JosephLee's post. :-)
In context, of course, it also seems probable that he is using
the word "sythesysize" to refer to what the compiler does,
internally. If I understand correctly, that's the whole point
of his book: what does the compiler do with the source code you
feed it?
I don't know the book, so I couldn't say what it's about.
Victor didn't agree with the original post. He agreed with what
Lippman said.
Okay. So my final words in this thread will be:

I was talking of something else and apparently I misunderstood what Victor
Bazarov said.

Hope that sorts it out :-)

Markus

Oct 31 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
22
by: Ruben Van Havermaet | last post by:
Hi, I have a problem using member functions in derived classes that override virtual member functions of base classes. The following pieces of (simplified) code don't work. Can anybody give...
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
8
by: pauldepstein | last post by:
I am writing a program which looks at nodes which have coordinates which are time-dependent. So I have a class called node which contains the private member declarations int date; int month; ...
75
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has...
10
by: Dennis Jones | last post by:
Hello, I have a hierarchy of classes in which there will be a data element that is common to all descendant classes. This element (a string in this case) is constant, but specific to each...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
3
by: Bob Altman | last post by:
Hi all, If I have a class that includes an instance of a struct as a member, how do I initialize that struct? I can't find a syntax for the constructor "initializer list" that works. For...
1
by: George2 | last post by:
Hello everyone, I am wondering in the following code, member variable a in class B is not put in the initialization list or constructor of B directly, but it is initialized. How and when...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.