473,385 Members | 1,409 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,385 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 4929
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...
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.