calling constructor when allocating an array

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

    calling constructor when allocating an array

    Hello, a very simple question:
    Ok I have a class MyClass with a constructor MyClass(int) (no constructor
    without argument defined)

    how can I make an array of pointers to objects of that class, calling the
    constructor with the index number as argument?

    <code>
    int N = 22;
    pointerArray = new MyClass*[N];
    for (int i=0; i< N; i++)
    pointerArray[i]->MyClass(i);
    </code>

    is this correct? does the second line call some default constructor for
    MyClass? Any better idea how to do that?

    Thanks Phil


  • Philipp

    #2
    Re: calling constructor when allocating an array

    Sorry my code was wrong... Is this correct?

    <code>

    int N = 22;
    pointerArray* MyClass;
    pointerArray = new MyClass[N];
    for (int i=0; i< N; i++)
    pointerArray[i]->MyClass(i);

    </code>



    Comment

    • Attila Feher

      #3
      Re: calling constructor when allocating an array

      Philipp wrote:[color=blue]
      > Sorry my code was wrong... Is this correct?
      >
      > <code>
      >
      > int N = 22;
      > pointerArray* MyClass;[/color]

      This is not a pointer array. It is a pointer to an array.
      [color=blue]
      > pointerArray = new MyClass[N];[/color]

      new creates an array of N pieces of objects of MyClass type.
      [color=blue]
      > for (int i=0; i< N; i++)
      > pointerArray[i]->MyClass(i);[/color]

      This is not good. The MyClass types are already constructed. And you
      cannot call constructors, they have no name. What do you want to do?

      --
      Attila aka WW


      Comment

      • Buster Copley

        #4
        Re: calling constructor when allocating an array

        Philipp wrote:
        [color=blue]
        > int N = 22;
        > pointerArray* MyClass;[/color]

        You mean 'MyClass * pointerArray;'. Why not post the real code?
        [color=blue]
        > pointerArray = new MyClass[N];[/color]

        This calls the default constructor for MyClass N times.
        [color=blue]
        > for (int i=0; i< N; i++)
        > pointerArray[i]->MyClass(i);[/color]

        Should be
        for (int i = 0; i < N; ++ i) pointerArray [i] = MyClass (i);
        or
        for (int i = 0; i < N; ++ i)
        {
        pointerArray [i].~MyClass ();
        new (& pointerArray [i]) MyClass (i);
        }

        If you allocate raw memory instead of objects:
        pointerArray = reinterpret_cas t <MyClass *>
        (new char [N * sizeof (MyClass)]);

        then you don't need the destructor call before the
        placement new inside the for loop. However, in that
        case you would need:

        for (int i = 0; i < N; ++ i) pointerArray [i].~MyClass ();
        delete [] reinterpret_cas t <char *> (pointerArray);

        instead of just 'delete [] pointerArray;'.

        Basically, the point is that you can't call a constructor on an
        object (because by the time it is an object, it has already been
        constructed).

        I hope this helps, and that I haven't made too many mistakes of my own.
        Regards,
        Buster.

        Comment

        • Buster Copley

          #5
          Re: calling constructor when allocating an array

          Attila Feher wrote:[color=blue][color=green]
          >>int N = 22;
          >>pointerArra y* MyClass;[/color]
          >
          > This is not a pointer array. It is a pointer to an array.[/color]

          No, it's a syntax error. A pointer to an object would look like this:
          MyClass * pointerArray;

          A pointer to an array would look like this:
          MyClass (* pointerArray) [NN];
          // NN is a (compile-time) integral constant

          Regards,
          Buster.

          Comment

          • Howard

            #6
            Re: calling constructor when allocating an array


            "Philipp" <_NO_S~P~A~M_ki tschen@hotmail. com> wrote in message
            news:3f686951$1 @epflnews.epfl. ch...[color=blue]
            > Sorry my code was wrong... Is this correct?
            >
            > <code>
            >
            > int N = 22;
            > pointerArray* MyClass;
            > pointerArray = new MyClass[N];
            > for (int i=0; i< N; i++)
            > pointerArray[i]->MyClass(i);
            >
            > </code>[/color]

            No. It would be much easier if you had a default constructor. Then you
            could just declare an array of objects instead of pointers. But to create
            an array of pointers, you first need to declare the array, then create
            instances of the objects for each array item to point to. You can't just
            call a constructor like a function...you have to "new" each pointer, like
            this:

            int N = 22;
            pointerArray* MyClass[N]; // no need to "new" this!
            for (int i = 0; i < N; ++i)
            pointerArray[i] = new MyClass(i); // create each instance!

            ....and, later, to delete...

            for (int i = (N-1); i >= 0; --i)
            delete pointerArray[i]; // delete each instance

            (BTW, you could count upwards in the delete loop, I just got in the practice
            long ago of deleting in the opposite order I allocated in, because on some
            systems it prevented memory fragmentation.. .but that's just me.)

            -Howard





            Comment

            • Ron Natalie

              #7
              Re: calling constructor when allocating an array


              "Philipp" <_NO_S~P~A~M_ki tschen@hotmail. com> wrote in message news:3f686951$1 @epflnews.epfl. ch...
              [color=blue]
              > int N = 22;
              > pointerArray* MyClass;
              > pointerArray = new MyClass[N];
              > for (int i=0; i< N; i++)
              > pointerArray[i]->MyClass(i);[/color]

              You can NOT call constructors at all. They are called for you as part
              of normal object creation. You can't create an array with other that
              default initialization. A vector, which is probably better suited for what
              you want to do anyhow, can be initialized with a non-default object, but
              it is the same for all elements.

              Besides, you're not initializing the array in the C++ sense. Initialization
              via the default constructor occurs when the new is invoked. But you
              can do what you are trying to do if you just put allt he stuff that would have
              been in your MyClass(int) constructor in a regular member function.

              Your class would look like:
              class MyClass {
              public:
              MyClass(); // default constructor required
              MyClass(int i) { Init(i); }

              void Init(int i);
              };

              vector<MyClass> pointerArray(N) ;
              for(int i = 0; i < N; ++i) pointerArray[i].Init(i);

              Now you don't even have to worry about deleting the array.


              Comment

              • Howard

                #8
                Re: calling constructor when allocating an array


                "Howard" <alicebt@hotmai l.com> wrote in message
                news:bk9r3g$577 @dispatch.conce ntric.net...[color=blue]
                >
                > pointerArray* MyClass[N]; // no need to "new" this![/color]


                DOH! Now he's goe ME doing it! :-) That should be (of course)

                MyClass* pointerArray[22];

                -Howard


                Comment

                • Clemens Auer

                  #9
                  Re: calling constructor when allocating an array

                  On Wed, 17 Sep 2003 16:02:05 +0200
                  "Philipp" <_NO_S~P~A~M_ki tschen@hotmail. com> wrote:
                  [color=blue]
                  > Sorry my code was wrong... Is this correct?
                  >
                  > <code>
                  >
                  > int N = 22;
                  > pointerArray* MyClass;
                  > pointerArray = new MyClass[N];
                  > for (int i=0; i< N; i++)
                  > pointerArray[i]->MyClass(i);
                  >
                  > </code>[/color]

                  i think you're trying to do something like this:

                  int N = 22;
                  // the ** makes the array of pointer not array of objects
                  MyClass **pointerArray;

                  // crerate all the Pointer
                  pointerArray = new (MyClass*)[N];

                  // create all the objects
                  for (int i=0; i< N; i++)
                  pointerArray[i] = new MyClass(i);


                  regards
                  Clemens

                  Comment

                  • Philipp

                    #10
                    Re: calling constructor when allocating an array

                    OK that helped a lot. Thank you (I'm still a bit confused about arrays and
                    pointers... hmmm, newbie perhaps? :-)


                    Comment

                    • Ron Natalie

                      #11
                      Re: calling constructor when allocating an array


                      "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message news:bk9q88$1o7 $1@newstree.wis e.edt.ericsson. se...
                      * MyClass;[color=blue]
                      >
                      > This is not a pointer array. It is a pointer to an array.[/color]

                      Well it's a pointer to the first element of an array.


                      Comment

                      • White Wolf

                        #12
                        Re: calling constructor when allocating an array

                        Ron Natalie wrote:[color=blue]
                        > "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
                        > news:bk9q88$1o7 $1@newstree.wis e.edt.ericsson. se... * MyClass;[color=green]
                        >>
                        >> This is not a pointer array. It is a pointer to an array.[/color]
                        >
                        > Well it's a pointer to the first element of an array.[/color]

                        Yes. This is the way it goes when we point to a part of the memory. We
                        point to the beginning of it. Like a pointer to a double will point to its
                        first byte. ;-)

                        --
                        WW aka Attila


                        Comment

                        • White Wolf

                          #13
                          Re: calling constructor when allocating an array

                          Buster Copley wrote:[color=blue]
                          > Attila Feher wrote:[color=green][color=darkred]
                          >>> int N = 22;
                          >>> pointerArray* MyClass;[/color]
                          >>
                          >> This is not a pointer array. It is a pointer to an array.[/color]
                          >
                          > No, it's a syntax error. A pointer to an object would look like this:
                          > MyClass * pointerArray;[/color]

                          Yeah. I missed that one. :-)
                          [color=blue]
                          > A pointer to an array would look like this:
                          > MyClass (* pointerArray) [NN];
                          > // NN is a (compile-time) integral constant[/color]

                          This is playing with the words.

                          MyClass *pointerArray;

                          will point to the array allocated by the new[] operator.

                          --
                          WW aka Attila


                          Comment

                          • Ron Natalie

                            #14
                            Re: calling constructor when allocating an array


                            "White Wolf" <wolof@freemail .hu> wrote in message news:bka12c$gu2 $1@phys-news1.kolumbus. fi...[color=blue]
                            > Ron Natalie wrote:[color=green]
                            > > "Attila Feher" <attila.feher@l mf.ericsson.se> wrote in message
                            > > news:bk9q88$1o7 $1@newstree.wis e.edt.ericsson. se... * MyClass;[color=darkred]
                            > >>
                            > >> This is not a pointer array. It is a pointer to an array.[/color]
                            > >
                            > > Well it's a pointer to the first element of an array.[/color]
                            >
                            > Yes. This is the way it goes when we point to a part of the memory. We
                            > point to the beginning of it. Like a pointer to a double will point to its
                            > first byte. ;-)
                            >[/color]
                            No, pointers point to complete objects as far as the language is concerend.
                            MyClass* points to one MyClass instance which happens to be the first
                            element of the array.



                            Comment

                            • Buster

                              #15
                              Re: calling constructor when allocating an array

                              > > A pointer to an array would look like this:[color=blue][color=green]
                              > > MyClass (* pointerArray) [NN];
                              > > // NN is a (compile-time) integral constant[/color]
                              >
                              > This is playing with the words.
                              >
                              > MyClass *pointerArray;
                              >
                              > will point to the array allocated by the new[] operator.[/color]

                              Sorry, no. To the first element. I know what you mean,
                              but it isn't what you said.


                              Comment

                              Working...