Topic: initializing array objects


Author: Edward Diener <eddielee@abraxis.com>
Date: 1997/09/25
Raw View
Arrays of objects are initialized with the default constructor.

Tony Bermudez wrote:

 > first of all thank you any replies.
 >
 > is it possible to initialize an array of objects, where the array is
 > a member of a class?
 >
 > ie.
 >
 > class A
 > {
 > public:
 >         A(int someValue);
 > private:
 >         int theValue;
 > };
 >
 > class B
 > {
 > public:
 >         B(int someValue);
 > private:
 >         A someAs[2];
 > };
 >
 > in the constructor for B I tried
 >
 > B::B(int someValue) : someAs[0](someValue), someAs[1](someValue)
 > {
 > }
 >
 > but it did not like it. My reference is Lippman's book and I could
 > not find there any references to this problem.
 >
 > I suspect it cannot be done, though I would appreciate confirmation of
 > this and maybe the reason why it cannot be done.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: stephen.clamage_nospam@eng.Sun.COM (Steve Clamage)
Date: 1997/09/25
Raw View
On 23 Sep 1997 10:09:56 PDT, bermudj@westminster.ac.uk (Tony Bermudez)
wrote:

>is it possible to initialize an array of objects, where the array is
>a member of a class?

Only default initialization is possible for a C-style array member of
a class. If the element type has a default constructor, it will be
used. If the element type does not, the code is in error. There is no
syntax to initialize an individual array element.

You can use an array class with appropriate constructors to get
non-default initialization.
Steve Clamage, stephen.clamage_nospam@eng.sun.com
( Note: remove "_nospam" when replying )
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1997/09/26
Raw View
Edward Diener <eddielee@abraxis.com> writes:

|>  Arrays of objects are initialized with the default constructor.

Not necessarily.  Although you cannot specify initialization when
new'ing an array, if the base type has a copy constructor, you can write
the following:

 MyType a[ 3 ] = { A( first ) , A( second , butWithTwoArgs ) , A() } ;

Of course, this doesn't work for non static members.

--
James Kanze    +33 (0)1 39 23 84 71    mailto: kanze@gabi-soft.fr
GABI Software, 22 rue Jacques-Lemercier, 78000 Versailles, France
        I'm looking for a job -- Je recherche du travail
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: bermudj@westminster.ac.uk (Tony Bermudez)
Date: 1997/09/23
Raw View
first of all thank you any replies.

is it possible to initialize an array of objects, where the array is
a member of a class?

ie.


class A
{
public:
 A(int someValue);
private:
 int theValue;
};

class B
{
public:
 B(int someValue);
private:
 A someAs[2];
};

in the constructor for B I tried

B::B(int someValue) : someAs[0](someValue), someAs[1](someValue)
{
}

but it did not like it. My reference is Lippman's book and I could
not find there any references to this problem.

I suspect it cannot be done, though I would appreciate confirmation of
this and maybe the reason why it cannot be done.

thank you again.


tony
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]