Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dynamic array and constructors

0 views
Skip to first unread message

Urs Thuermann

unread,
Oct 31, 2008, 4:42:20 AM10/31/08
to
I have some old code I've written several years ago that doesn't
compile with newer versions of GCC. The code allocates an array of
objects that need to be initialized by calling a constructor with one
argument:

class B;

class A {
B *b;

public:
A(B *p) : b(p) {}
};

class B {

public:
void foo() {
// this declaration is ok
A a(this);

// the following causes an error with newer GCC:
// error: ISO C++ forbids initialization in array new
A *arr = new A[10](this);
}
};

int main()
{
B b;
}

This worked with g++ until version 3.3.x, but not since 3.4.x. The
problem is in the expression new A[10](this), since according to GCC,
initialization in array new is forbidden.

How would I initialize the array elements in ISO C++?


urs

Maxim Yegorushkin

unread,
Oct 31, 2008, 5:34:02 AM10/31/08
to
On Oct 31, 8:42 am, Urs Thuermann <u...@janus.isnogud.escape.de>
wrote:

There is no way to specify constructor arguments when using the array
version of new. The default constructor must be available.

Please see the older thread
http://groups.google.co.uk/group/comp.lang.c++.moderated/browse_frm/thread/18f1c9313d32d315
for more information.

--
Max

James Kanze

unread,
Oct 31, 2008, 8:05:52 AM10/31/08
to
On Oct 31, 9:42 am, Urs Thuermann <u...@janus.isnogud.escape.de>
wrote:

> class B;

> class A {
> B *b;

> public:
> A(B *p) : b(p) {}
> };

> int main()
> {
> B b;
> }

It's always been forbidden.

> How would I initialize the array elements in ISO C++?

std::vector< A > v( 10, this ) ;

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Juha Nieminen

unread,
Oct 31, 2008, 11:01:07 AM10/31/08
to
James Kanze wrote:
>> How would I initialize the array elements in ISO C++?
>
> std::vector< A > v( 10, this ) ;

That is, of course, the best and safest way of doing it.

OTOH, it might be interesting to know how std::vector does this.
(After all, std::vector *does* allocate space for a certain amount of
elements without needing a default constructor for those elements.) It
goes something like this:

// Requires #include <memory>
A* array = std::allocator<A>().allocate(10);
for(int i = 0; i < 10; ++i)
new(array+i) A(this);

Of course I'm not recommending you to do it like this. Use std::vector
instead.

Maxim Yegorushkin

unread,
Oct 31, 2008, 12:20:24 PM10/31/08
to
On Oct 31, 3:01 pm, Juha Nieminen <nos...@thanks.invalid> wrote:
> James Kanze wrote:
> >> How would I initialize the array elements in ISO C++?
>
> >     std::vector< A > v( 10, this ) ;
>
>   That is, of course, the best and safest way of doing it.
>
>   OTOH, it might be interesting to know how std::vector does this.

No magic here: allocate raw memory and then invoke the constructors
manually.

std::uninitialized_fill() is used in some std::vector<>
implementations for the second step. http://www.sgi.com/tech/stl/uninitialized_fill.html

--
Max

0 new messages