Non default constructor for array objects

Discussion in 'C++' started by Denis Remezov, Apr 13, 2004.

  1. 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
    Denis Remezov, Apr 13, 2004
    #1
    1. Advertising

  2. 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
    Kutty Banerjee, Apr 13, 2004
    #2
    1. Advertising

  3. Denis Remezov

    Buster Guest

    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.
    Buster, Apr 13, 2004
    #3
  4. * Buster <> 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 = "Wow!"; }

    std::vector<std::string> b( 50 );
    for( unsigned i = 0; i < 50; ++i ) { b = "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?
    Alf P. Steinbach, Apr 13, 2004
    #4
  5. Denis Remezov

    Buster Guest

    Alf P. Steinbach wrote:

    > * Buster <> 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 = "Wow!"; }
    >
    > std::vector<std::string> b( 50 );
    > for( unsigned i = 0; i < 50; ++i ) { b = "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.
    Buster, Apr 13, 2004
    #5
  6. * Buster <> schriebt:
    > Alf P. Steinbach wrote:
    >
    > > * Buster <> 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?
    Alf P. Steinbach, Apr 13, 2004
    #6
  7. Denis Remezov

    Buster Guest

    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.
    Buster, Apr 13, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aire
    Replies:
    3
    Views:
    463
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    9
    Views:
    959
    Alf P. Steinbach
    Mar 6, 2006
  3. desktop
    Replies:
    2
    Views:
    454
    Victor Bazarov
    Jun 6, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,217
  5. yatko
    Replies:
    3
    Views:
    2,511
    yatko
    Dec 28, 2008
Loading...

Share This Page