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

Initializing an array of objects allocated dynamically

P: n/a
DK
Is there any pointer analog to this code (adapted from the FAQ):
someclass Objects[5] = { someclass(1), someclass(2), someclass(3),
someclass(4), someclass(5) }; Also, does the above code require the
copy constructor and/or assignment operator to be public? Considering
the fact that someclass has only parameterized constructors (so
creating someclass *pObjects = new someclass[5]; is not an option)?
The idea behind this is that I want to create a class which is
immutable and non-copyable after construction. Hence I don't want to
make the copy constructor and operator = public.

Mar 21 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Mar 21, 2:35 pm, "DK" <divyekap...@gmail.comwrote:
Is there any pointer analog to this code (adapted from the FAQ):
someclass Objects[5] = { someclass(1), someclass(2), someclass(3),
someclass(4), someclass(5) }; Also, does the above code require the
copy constructor and/or assignment operator to be public? Considering
the fact that someclass has only parameterized constructors (so
creating someclass *pObjects = new someclass[5]; is not an option)?
The idea behind this is that I want to create a class which is
immutable and non-copyable after construction. Hence I don't want to
make the copy constructor and operator = public.
Do you mean something like:

class someclass
{
someclass& operator=( const someclass& );
someclass( const someclass& );
public:
someclass(int) {}
};

someclass *Objects[] =
{
new someclass(1),
new someclass(2),
new someclass(3)
};

or

auto_ptr<someclassObjects[] =
{
auto_ptr<someclass>( new someclass(1) ),
auto_ptr<someclass>( new someclass(2) ),
auto_ptr<someclass>( new someclass(3) )
};

Cheers! --M

Mar 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.