[comp.lang.c++] Initializing an array class member?

lfung@alias.UUCP (Lisa A. Fung) (02/05/91)

What is the correct syntax for initializing an array class member?
I have tried some combinations, all of which have resulted in syntax
errors with the MacIntosh MPW C++ compiler.  If you have an answer,
please e-mail me.  Also, if I happened to miss the answer in some C++ book
or other, could you include the reference and page number?

Here's my example:

class myClass {
  public:
    myClass();  // constructor
    ~myClass(); // destructor
  private:
    Tthing   arrayOfThings[3];  // Tthing's constructor takes an arg...
};

//
//  The following attempt at initializing leads to a compile error...
//
myClass::myClass()
:   arrayOfThings[0](  arg),
    arrayOfThings[1](  arg),
    arrayOfThings[2](  arg)
{}

//
//  This attempt also leads to a compile error...
//
myClass::myClass()
:   arrayOfThings(  Tthing( arg), TThing( arg), Tthing( arg))
{}

steve@taumet.com (Stephen Clamage) (02/06/91)

lfung@alias.UUCP (Lisa A. Fung) writes:

>class myClass {
>  public:
>    myClass();  // constructor
>    ~myClass(); // destructor
>  private:
>    Tthing   arrayOfThings[3];  // Tthing's constructor takes an arg...
>};

To initialize an array of objects whose constructor requires arguments,
you must provide complete initialization (E&S 12.6.1).  If a class has
a default constructor (one requiring no arguments), you do not need
to provide initializers.  For example:

struct T1 { T1(int); };		// no default constructor
struct T2 { T2(); T2(int); };	// has a default constructor

T1 a[3];			// illegal, no default constructor to call
T1 b[3] = { T1(1), T1(2), T1(3) };	// ok

T2 c[3];			// ok, uses default constructor
T2 d[3] = { T2(1), T2(2), T2(3) };	// ok
-- 

Steve Clamage, TauMetric Corp, steve@taumet.com

rae@utcs.toronto.edu (Reid Ellis) (02/07/91)

lfung@alias.UUCP (Lisa A. Fung) writes:
>class myClass {
>  public:
>    myClass();  // constructor
>    ~myClass(); // destructor
>  private:
>    Tthing   arrayOfThings[3];  // Tthing's constructor takes an arg...
>};

The above is a class containing an array.

steve@taumet.com (Stephen Clamage) writes:
>struct T1 { T1(int); };		// no default constructor
>struct T2 { T2(); T2(int); };	// has a default constructor
>
>T1 a[3];			// illegal, no default constructor to call
>T1 b[3] = { T1(1), T1(2), T1(3) };	// ok
>
>T2 c[3];			// ok, uses default constructor
>T2 d[3] = { T2(1), T2(2), T2(3) };	// ok

The above is an array of class objects.

This still doesn't answer the question.  How do you write T1::T1() if
you have an array *IN THE CLASS*.  i.e.:

	struct T1 { T1(); int a[10]; }

	T1::T1() : a(???) { }

What do you put for "???"?
					Reid
--
Reid Ellis  176 Brookbanks Drive, Toronto ON, M3A 2T5 Canada
rae@gpu.utcs.toronto.edu      ||  rae%alias@csri.toronto.edu
CDA0610@applelink.apple.com   ||             +1 416 446 1644

jimad@microsoft.UUCP (Jim ADCOCK) (02/13/91)

In article <1991Feb4.215231.16157@alias.uucp> lfung@alias.UUCP (Lisa A. Fung) writes:
|
|What is the correct syntax for initializing an array class member?
|I have tried some combinations, all of which have resulted in syntax
|errors with the MacIntosh MPW C++ compiler.  If you have an answer,
|please e-mail me.  Also, if I happened to miss the answer in some C++ book
|or other, could you include the reference and page number?
|
|Here's my example:
|
|class myClass {
|  public:
|    myClass();  // constructor
|    ~myClass(); // destructor
|  private:
|    Tthing   arrayOfThings[3];  // Tthing's constructor takes an arg...
|};

According to ARM pgs 289-290, your choices are to provide a default
constructor for Tthing, or supply values inside the constructor as follows:

myClass::myClass()
{
	arrayOfThings[0] = 0;
	arrayOfThings[1] = 100;
	arrayOfThings[2] = 200;
}

This approach would seem to imply that the array is to be default 
initialized first, which would imply you must have a default Tthing
constructor.

The ARM goes on to note that there is no syntax for nonstatic const
arrays, which I imply to mean that the above are your only two choices
for nonstatic nonconst member arrays.