Initializing an array of objects allocated dynamically

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • DK

    #1

    Initializing an array of objects allocated dynamically

    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.

  • mlimber

    #2
    Re: Initializing an array of objects allocated dynamically

    On Mar 21, 2:35 pm, "DK" <divyekap...@gm ail.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<somecl assObjects[] =
    {
    auto_ptr<somecl ass>( new someclass(1) ),
    auto_ptr<somecl ass>( new someclass(2) ),
    auto_ptr<somecl ass>( new someclass(3) )
    };

    Cheers! --M

    Comment

    Working...