sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
kiryazev@gmail.com's Avatar

new-initializers for an array


Question posted by: kiryazev@gmail.com (Guest) on November 10th, 2006 04:45 PM
Hello. Is the following code correct in standard c++?

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

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

8 Answers Posted
Victor Bazarov's Avatar
Guest - n/a Posts
#2: Re: new-initializers for an array

Join Bytes! wrote:
Quote:
Originally Posted by
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


Davlet Panech's Avatar
Guest - n/a Posts
#3: Re: new-initializers for an array

Join Bytes! wrote:
Quote:
Originally Posted by
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.
Dan Bloomquist's Avatar
Guest - n/a Posts
#4: Re: new-initializers for an array



Join Bytes! wrote:
Quote:
Originally Posted by
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.

kiryazev@gmail.com's Avatar
kiryazev@gmail.com November 10th, 2006 05:45 PM
Guest - n/a Posts
#5: Re: new-initializers for an array

"""Victor Bazarov ΠΙΣΑΜ(Α):
"""
Quote:
Originally Posted by
Join Bytes! wrote:
Quote:
Originally Posted by
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?

Victor Bazarov's Avatar
Guest - n/a Posts
#6: Re: new-initializers for an array

Join Bytes! wrote:
Quote:
Originally Posted by
"""Victor Bazarov ΠΙΣΑΜ(Α):
"""
Quote:
Originally Posted by
>Join Bytes! wrote:
Quote:
Originally Posted by
>>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


Andrey Tarasevich's Avatar
Andrey Tarasevich November 10th, 2006 06:15 PM
Guest - n/a Posts
#7: Re: new-initializers for an array

Join Bytes! wrote:
Quote:
Originally Posted by
Quote:
Originally Posted by
>...
>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
Salt_Peter's Avatar
Guest - n/a Posts
#8: Re: new-initializers for an array


Join Bytes! wrote:
Quote:
Originally Posted by
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);

benben's Avatar
Guest - n/a Posts
#9: Re: new-initializers for an array

Join Bytes! wrote:
Quote:
Originally Posted by
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
 
Not the answer you were looking for? Post your question . . .
197,012 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 197,012 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors