Non default constructor for array objects

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

    Non default constructor for array objects

    Hi,
    MyClass *myclass_=new MyClass[100];

    and MyClass::MyClas s(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


  • Buster

    #2
    Re: Non default constructor for array objects

    Kutty Banerjee wrote:
    [color=blue]
    > MyClass *myclass_=new MyClass[100];[/color]

    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_cas t, or using std::allocator.

    --
    Regards,
    Buster.

    Comment

    • Denis Remezov

      #3
      Re: Non default constructor for array objects

      Kutty Banerjee wrote:[color=blue]
      >
      > Hi,
      > MyClass *myclass_=new MyClass[100];
      >
      > and MyClass::MyClas s(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[/color]

      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

      Comment

      • Alf P. Steinbach

        #4
        Re: Non default constructor for array objects

        * Buster <noone@nowhere. com> schriebt:[color=blue]
        > Kutty Banerjee wrote:
        >[color=green]
        > > MyClass *myclass_=new MyClass[100];[/color][/color]

        Don't use an array, use e.g. std::vector.

        [color=blue]
        > You cannot create an array-of-T if T has no default constructor.[/color]

        std::string const a[] = { "Hey", "Ho", "Here", "We", "Go" };

        [color=blue]
        > Therefore, if you insist on an array then you must provide a default
        > constructor[/color]

        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_p tr.

        [color=blue]
        > and if you cannot provide a default constructor then you cannot create an
        > array.[/color]

        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?

        Comment

        • Buster

          #5
          Re: Non default constructor for array objects

          Alf P. Steinbach wrote:
          [color=blue]
          > * Buster <noone@nowhere. com> schriebt:
          >[color=green]
          >>Kutty Banerjee wrote:
          >>[color=darkred]
          >>> MyClass *myclass_=new MyClass[100];[/color][/color]
          >
          > Don't use an array, use e.g. std::vector.[/color]

          Good advice.
          [color=blue][color=green]
          >>You cannot create an array-of-T if T has no default constructor.[/color]
          >
          > std::string const a[] = { "Hey", "Ho", "Here", "We", "Go" };[/color]

          OK, I see.
          [color=blue][color=green]
          >>Therefore, if you insist on an array then you must provide a default
          >>constructor[/color]
          >
          > 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:[/color]

          You can't do this. If you had used a type without a default constructor
          your compiler would have complained.
          [color=blue]
          > 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_p tr.
          >[color=green]
          >>and if you cannot provide a default constructor then you cannot create an
          >>array.[/color]
          >
          > That turns out not to be the case... ;-)[/color]

          Indeed.

          --
          Regards,
          Buster.

          Comment

          • Alf P. Steinbach

            #6
            Re: Non default constructor for array objects

            * Buster <noone@nowhere. com> schriebt:[color=blue]
            > Alf P. Steinbach wrote:
            >[color=green]
            > > * Buster <noone@nowhere. com> schriebt:
            > >[color=darkred]
            > >>Kutty Banerjee wrote:
            > >>
            > >>> MyClass *myclass_=new MyClass[100];[/color]
            > >
            > > Don't use an array, use e.g. std::vector.[/color]
            >
            > Good advice.
            >[color=green][color=darkred]
            > >>You cannot create an array-of-T if T has no default constructor.[/color]
            > >
            > > std::string const a[] = { "Hey", "Ho", "Here", "We", "Go" };[/color]
            >
            > OK, I see.
            >[color=green][color=darkred]
            > >>Therefore, if you insist on an array then you must provide a default
            > >>constructor[/color]
            > >
            > > 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:[/color]
            >
            > You can't do this. If you had used a type without a default constructor
            > your compiler would have complained.[/color]

            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?

            Comment

            • Buster

              #7
              Re: Non default constructor for array objects

              Buster wrote:

              [...]
              [color=blue]
              > You can work with uninitialized memory, either with arrays of some
              > built-in type and reinterpret_cas t, or using std::allocator.[/color]

              I should have said, "with arrays of some built-in type,
              reinterpret_cas t, placement new and explicit destructor calls".
              std::allocator can be used instead of all that.

              --
              Regards,
              Buster.

              Comment

              Working...