473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Non default constructor for array objects

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
Jul 22 '05 #1
6 2388
Kutty Banerjee wrote:
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.

--
Regards,
Buster.
Jul 22 '05 #2
Kutty Banerjee wrote:

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
Jul 22 '05 #3
* Buster <no***@nowhere.com> schriebt:
Kutty Banerjee wrote:
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[i] = "Wow!"; }

std::vector<std::string> b( 50 );
for( unsigned i = 0; i < 50; ++i ) { b[i] = "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... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #4
Alf P. Steinbach wrote:
* Buster <no***@nowhere.com> schriebt:
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.
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:


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[i] = "Wow!"; }

std::vector<std::string> b( 50 );
for( unsigned i = 0; i < 50; ++i ) { b[i] = "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.

--
Regards,
Buster.
Jul 22 '05 #5
* Buster <no***@nowhere.com> schriebt:
Alf P. Steinbach wrote:
* Buster <no***@nowhere.com> schriebt:
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.
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:


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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #6
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.

--
Regards,
Buster.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including...
6
by: Kutty Banerjee | last post by:
Hi, MyClass *myclass_=new MyClass; 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...
17
by: highli | last post by:
When a non-default constructor provided in a class, the default constructor is not available anymore. In what cases shall a default constructor be defined explicitly? Specifically, in the...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
16
by: Steven T. Hatton | last post by:
As far as I know, there is no way to provide a default value for the argument to the constructor A::A(char (&array)) in this example. Correct? struct A{ A(char (&array) ){...
7
by: John Grandy | last post by:
make a call to XML Web Service WebMethod ... returns object myArray with no error ... myArray contains objects of type StringKeyStringValue runtime error occurs on accessing properties of...
3
by: swengtoo | last post by:
In his book "More Effective C++", Scott Meyer suggests in "Item 4" to "Avoid gratuitous default constructors". To summarize his claims against default constructors for "the right classes" he...
1
by: crabhunt | last post by:
Hi ! Is it possible to create an array of objects of a class with a constructor that takes some arguments ? If so what is declaration like ? I mean say I have class ABC{ int i; ...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
by: Jess | last post by:
Hello, I tried several books to find out the details of object initialization. Unfortunately, I'm still confused by two specific concepts, namely default-initialization and...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.