By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

new-initializers for an array

P: n/a
Hello. Is the following code correct in standard c++?

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

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

Nov 10 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
ki******@gmail.com wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 10 '06 #2

P: n/a
ki******@gmail.com wrote:
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.
Nov 10 '06 #3

P: n/a


ki******@gmail.com wrote:
Hello. Is the following code correct in standard c++?

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

int main()
{
A* p = new A[10](99);
}
And:
<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.5>

Best, Dan.

Nov 10 '06 #4

P: n/a
"""Victor Bazarov ΠΙΣΑΜ(Α):
"""
ki******@gmail.com wrote:
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?

Nov 10 '06 #5

P: n/a
ki******@gmail.com wrote:
"""Victor Bazarov ΠΙΣΑΜ(Α):
"""
>ki******@gmail.com wrote:
>>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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 10 '06 #6

P: n/a
ki******@gmail.com wrote:
>...
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

--
Best regards,
Andrey Tarasevich
Nov 10 '06 #7

P: n/a

ki******@gmail.com wrote:
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);

Nov 10 '06 #8

P: n/a
ki******@gmail.com wrote:
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
Nov 12 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.