new-initializers for an array

K

kiryazev

Hello. Is the following code correct in standard c++?

struct A
{
A(){}
A(int){}
};

int main()
{
A* p = new A[10](99);
}
 
V

Victor Bazarov

Hello. Is the following code correct in standard c++?

struct A
{
A(){}
A(int){}
};

int main()
{
A* p = new A[10](99);
}

No. There can be no expression between the parentheses
following the bracketed expression in the 'new' expression.
It can only be

A* p = new A[10]();

If you need to initialise all your elements to a particular
value (int in your case), you would have to use some kind
of "indirect initialiser" trick, like this:

#include <iostream>
struct A
{
static int initialiser;
A(int i = initialiser) // this could be used as
{ // your default c-tor as well
std::cout << i << std::endl;
}

};

int A::initialiser = 42;

int main()
{
A* p42 = new A[10](); // initialises with what's there
A::initialiser = 99; // set the "what's there" to 99
A* p99 = new A[10](); // initialises with what's there
}

V
 
D

Davlet Panech

Hello. Is the following code correct in standard c++?

struct A
{
A(){}
A(int){}
};

int main()
{
A* p = new A[10](99);
}

No, arrays are always initialized with the default ctor.

D.
 
K

kiryazev

"""Victor Bazarov ÐÉÓÁÌ(Á):
"""
Hello. Is the following code correct in standard c++?

struct A
{
A(){}
A(int){}
};

int main()
{
A* p = new A[10](99);
}

No. There can be no expression between the parentheses
following the bracketed expression in the 'new' expression.
It can only be

A* p = new A[10]();

Could you please point to the relevant paragraph?
 
V

Victor Bazarov

"""Victor Bazarov ÐÉÓÁÌ(Á):
"""
Hello. Is the following code correct in standard c++?

struct A
{
A(){}
A(int){}
};

int main()
{
A* p = new A[10](99);
}

No. There can be no expression between the parentheses
following the bracketed expression in the 'new' expression.
It can only be

A* p = new A[10]();

Could you please point to the relevant paragraph?

5.3.4/15.

V
 
A

Andrey Tarasevich

...
No. There can be no expression between the parentheses
following the bracketed expression in the 'new' expression.
It can only be

A* p = new A[10]();

Could you please point to the relevant paragraph?
...

5.3.4/15
 
S

Salt_Peter

Hello. Is the following code correct in standard c++?

struct A
{
A(){}
A(int){}
};

int main()
{
A* p = new A[10](99);
}

No, but if you need the above, simply do it in the default ctor.

struct A
{
int a;
public:
A() : a(99) { }
};

// And you don't need new at all.

int main()
{
A array[10]; // 10 elements set to 99
}

A better alternative: std::vector< int > vn(10, 99);
 
B

benben

Hello. Is the following code correct in standard c++?

struct A
{
A(){}
A(int){}
};

int main()
{
A* p = new A[10](99);
}

Add a copy constructor to A then take a look at std::allocator and
std::vector

Regards,
Ben
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top