dynamic array and constructors

U

Urs Thuermann

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
 
M

Maxim Yegorushkin

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

James Kanze

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 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 ) ;
 
J

Juha Nieminen

James said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top