Non default constructor for array objects

D

Denis Remezov

Kutty said:
Hi,
MyClass *myclass_=new MyClass[100];

and MyClass::MyClass(int) and no default constructor. My object assignment
obviously gives an error. So waht is the correct way to do it without using
vectors of course.

kutty

Technically, it is possible to use placement new to construct objects of MyClass
on a previously allocated pool. You would then call new(address) MyClass(value)
iteratively and you could even pass a different value to the constructor each
time. There are a couple of issues with that approach that might not be
immediately obvious. The syntax for destruction is different from normal
as well. See FAQ 11.10 and 11.14 (www.parashift.com/c++-faq-lite).

Practically, placement new should better be avoided if there is a sensible
alternative. Why not use a vector?

Denis
 
K

Kutty Banerjee

Hi,
MyClass *myclass_=new MyClass[100];

and MyClass::MyClass(int) and no default constructor. My object assignment
obviously gives an error. So waht is the correct way to do it without using
vectors of course.

kutty
 
B

Buster

Kutty said:
MyClass *myclass_=new MyClass[100];

You cannot create an array-of-T if T has no default constructor.
Therefore, if you insist on an array then you must provide a default
constructor, and if you cannot provide a default constructor then you
cannot create an array.

You can work with uninitialized memory, either with arrays of some
built-in type and reinterpret_cast, or using std::allocator.
 
A

Alf P. Steinbach

* Buster said:
Kutty said:
MyClass *myclass_=new MyClass[100];

Don't use an array, use e.g. std::vector.

You cannot create an array-of-T if T has no default constructor.

std::string const a[] = { "Hey", "Ho", "Here", "We", "Go" };

Therefore, if you insist on an array then you must provide a default
constructor

No that is not necessary. But with direct storage in the array an initializer
must be provided. Alternatively, as Deniz Remezov remarked, it's possible to
use placement new, but best avoided. Alternatively, just copy the objects
into the array:


std::string a[50];
for( unsigned i = 0; i < 50; ++i ) { a = "Wow!"; }

std::vector<std::string> b( 50 );
for( unsigned i = 0; i < 50; ++i ) { b = "Wow!"; }


Or, for example, use a std::vector of boost::shared_ptr.

and if you cannot provide a default constructor then you cannot create an
array.

That turns out not to be the case... ;-)
 
B

Buster

Alf said:
* Buster said:
Kutty said:
MyClass *myclass_=new MyClass[100];

Don't use an array, use e.g. std::vector.

Good advice.
You cannot create an array-of-T if T has no default constructor.

std::string const a[] = { "Hey", "Ho", "Here", "We", "Go" };

OK, I see.
No that is not necessary. But with direct storage in the array an initializer
must be provided. Alternatively, as Deniz Remezov remarked, it's possible to
use placement new, but best avoided. Alternatively, just copy the objects
into the array:

You can't do this. If you had used a type without a default constructor
your compiler would have complained.
std::string a[50];
for( unsigned i = 0; i < 50; ++i ) { a = "Wow!"; }

std::vector<std::string> b( 50 );
for( unsigned i = 0; i < 50; ++i ) { b = "Wow!"; }

Or, for example, use a std::vector of boost::shared_ptr.
and if you cannot provide a default constructor then you cannot create an
array.

That turns out not to be the case... ;-)


Indeed.
 
A

Alf P. Steinbach

* Buster said:
Alf said:
* Buster said:
Kutty Banerjee wrote:

MyClass *myclass_=new MyClass[100];

Don't use an array, use e.g. std::vector.

Good advice.
You cannot create an array-of-T if T has no default constructor.

std::string const a[] = { "Hey", "Ho", "Here", "We", "Go" };

OK, I see.
No that is not necessary. But with direct storage in the array an initializer
must be provided. Alternatively, as Deniz Remezov remarked, it's possible to
use placement new, but best avoided. Alternatively, just copy the objects
into the array:

You can't do this. If you had used a type without a default constructor
your compiler would have complained.

Right. Sorry. That technique requires placement new, again.

Ouch.

And thanks.
 
B

Buster

Buster wrote:

[...]
You can work with uninitialized memory, either with arrays of some
built-in type and reinterpret_cast, or using std::allocator.

I should have said, "with arrays of some built-in type,
reinterpret_cast, placement new and explicit destructor calls".
std::allocator can be used instead of all that.
 

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,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top