[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

dynamic array and constructors

Urs Thuermann

10/31/2008 8:42:00 AM

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
4 Answers

Maxim Yegorushkin

10/31/2008 9:34:00 AM

0

On Oct 31, 8:42 am, Urs Thuermann <u...@janus.isnogud.escape.de>
wrote:
> 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++?

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/18f1c9...
for more information.

--
Max

James Kanze

10/31/2008 12:06:00 PM

0

On Oct 31, 9:42 am, Urs Thuermann <u...@janus.isnogud.escape.de>
wrote:
> 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.

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.kanze@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

10/31/2008 3:01:00 PM

0

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

10/31/2008 4:20:00 PM

0

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...

--
Max