Re: About member variable initilization and default constructor issues
On Oct 30, 4:49 pm, JosephLee <shoupe...@gmail.com> 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:
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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34