Initializing an array of objects allocated dynamically

D

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.
 
M

mlimber

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<someclass> Objects[] =
{
auto_ptr<someclass>( new someclass(1) ),
auto_ptr<someclass>( new someclass(2) ),
auto_ptr<someclass>( new someclass(3) )
};

Cheers! --M
 

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

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top