[comp.lang.c++] Call of constructors by vectors

tma@mas1.UUCP (Tim Atkins) (06/09/89)

For the life of me, I don't understand why

	SomeClass x[100];

needs to invoke SomeClass::SomeClass when

	simpleCType x[100];

has no need to do such.  My intuition would be that in the former case
I have merely allocated space for 100 instances of SomeClass and that no
initialization was asked for or even desirable.

Can someone explain this feature to me?  I fail to see the current C++
behaviour as a desirable feature.

thanks,
Tim Atkins

jima@hplsla.HP.COM (Jim Adcock) (06/10/89)

Well,
	SomeClass x1;

needs to invoke SomeClass::SomeClass whereas

	simpleCType x1;

has no need to do such.  
A better question is why does:

	SomeClass x[100];

with:

inline SomeClass::SomeClass(){}

cause 100 invocations of this do-nothing "initializer" whereas x1 does not
cause even 1 invocation of the do-nothing "initializer".

shankar@hpclscu.HP.COM (Shankar Unni) (06/13/89)

> A better question is why does:
> 	SomeClass x[100];
> with:
>       inline SomeClass::SomeClass(){}
> cause 100 invocations of this do-nothing "initializer" whereas x1 does not
> cause even 1 invocation of the do-nothing "initializer".

Lazy code generation...

No, I take that back. This is a solvable problem, but I suppose that the
ROI for doing it the most efficient way was not seen as making it
worthwhile to do. Who knows.. In general, it's easy to do the trivial case
of the null constructor (I mean a constructor with an empty body). If the
constructor has even 1 statement, it becomes pretty hard.

A better solution would have been an array constructor syntax. Maybe
something like:

   SomeClass::SomeClass[int count]() { ... }

Naah! :-) (Parsing C++ is already a pain in the ***; no need to make it any
worse).

But seriously, it would address this problem. Only one function is called
with a pointer to the array and a number of elements, and whatever other
parameters the user specifies.
----
Shankar.

simpson@poseidon.uucp (Scott Simpson) (06/13/89)

>	simpleCType x1;
>
>has no need to do such.  
>A better question is why does:
>
>	SomeClass x[100];
>
>with:
>
>inline SomeClass::SomeClass(){}
>
>cause 100 invocations of this do-nothing "initializer" whereas x1 does not
>cause even 1 invocation of the do-nothing "initializer".

.
.
.
.
.  Stupid inews line count "feature"
.
.
.
.
.
Because the SomeClass initializer may do a "new" and you want 100 objects 
allocated.  See Stroustrup section 5.5.5 page 162.  What do I win? :-)
	Scott Simpson
	TRW Space and Defense Sector
	oberon!trwarcadia!simpson  	(UUCP)
	trwarcadia!simpson@usc.edu	(Internet)

jima@hplsla.HP.COM (Jim Adcock) (06/15/89)

> >inline SomeClass::SomeClass(){}
> >
> Because the SomeClass initializer may do a "new" and you want 100 objects 
> allocated.  See Stroustrup section 5.5.5 page 162.  What do I win? :-)

How can the SomeClass initializer do a "new" when I pointed out its not
doing anything?  I was arguing that I would at least expect a compiler
to recognize when a class has a no-op initializer and not bother calling
it 100 times ???