Topic: Initializing private member array
Author: ronb@burklabs (Ron Burk )
Date: 22 Aug 91 05:11:34 GMT Raw View
I asked the question below on comp.lang.c++, but I failed to
successfully communicate what I am after. I am not interested in what
any particular C++ compiler does with the code example shown below. I
am interested in whether or not the ARM *intends* the code example to be
legal. My feeling is that the ARM is too vague on this point and should
explicitly affirm or deny that private constructors are available within
the initialization list of a member array. I am hoping that someone
will either agree that this is an ambiguity or point to ARM text that
they think covers the situation. Page 289 (1990 ARM) shows that the
example code is legal if the constructor is public and page 304 allows
static members of such a class to create objects of that class.
However, I am not real clear on whether the compiler is allowed and
required to use the private constructor to create a temporary to
initialize the private, static member (given that there are cases where
individual compilers require a temporary).
Here is the discussion that already transpired on comp.lang.c++
R. Michael Anderson writes:
> Regarding the following code from Ron Burk
>
> class Root {
> Root(int BinNumber);
> int BinNumber;
> static Root Roots[5];
> };
> Root Root::Roots[5] =
> { 1, 2, 3, 4, 5 };
>
> Steve Clamage writes:
>
> > 1. You cannot initialize an array of objects unless the object type has a
> > constructor requiring no arguments.
>
> and Larry Streepy says:
>
> > 1) The first major problem with the code is the way you attempted to
> > initialize an array of root objects. You can't initialize an array of
> > objects that way. To have an array of objects you MUST declare a
> > constructor with no parameters.
>
> Both comments reflect the way Cfront behaves (and other C++ compilers, too),
> but not the language definition contained in the ARM (and the current
> version of the draft standard from X3J16). Take a look at section 12.6.1
> of the ARM:
>
> Arrays of objects of a class with constructors use constructors in
> initialization just like individual objects. . . .
>
> An example follows:
>
> complex v[6] = { 1, complex(1,2), complex(), 2 };
>
> which assumes the existence of constructors complex::complex(),
> complex::complex(double), and complex::complex(double,double), with the
> default constructor used for v[4] and v[5] as well as for v[2].
>
> So, unless I overlooked a special case for static data members, Ron's
> program is legal according to the ARM. But, of course, that doesn't
> do him much good if his compiler doesn't accept it.
I am not at all sure that a compiler writer following the ARM would
produce a compiler that accepts this:
extern int v,w,x,y;
class A {
A(int a, int b);
static A global[2];
};
A A::global[2] = { A(v,w), A(x,y) };
but I feel the language should support this (barring any typos I've
overlooked).
[Ron Burk, ronb@burklabs, uunet!seaeast.wa.com!burklabs!ronb, CIS 70302,2566]