Copy Constructor for an Array Element ???

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

    Copy Constructor for an Array Element ???

    //
    // Is there something wrong with my syntax for the
    // Copy Constructor of an Array Element, or does
    // the C++ language not support this?
    //

    #include <stdio.h>
    #include <stdlib.h>

    class X {
    int Data;
    public:
    X() { Data = 56; };
    X(const X& Y) { Data = Y.Data; }; // Copy Constructor
    void Construct(const X& Y) { Data = Y.Data; };
    };

    int main()
    {
    X ABC;
    X* Temp = (X*) malloc(sizeof(X ) * 10);
    for (int N = 0; N < 10; N++) {
    // Temp[N].(ABC); // Does Not Compile
    // Temp[N](ABC); // Does Not Compile
    Temp[N].Construct(ABC) ; // Compiles and Executes Correctly
    }
    free(Temp);
    return 0;
    }



  • Victor Bazarov

    #2
    Re: Copy Constructor for an Array Element ???

    "Peter Olcott" <olcott@worldne t.att.net> wrote...[color=blue]
    > //
    > // Is there something wrong with my syntax for the
    > // Copy Constructor of an Array Element, or does
    > // the C++ language not support this?[/color]

    The latter. Constructors do not have names, therefore they cannot
    be found during name lookup and hence cannot be _called_. They can
    only be _invoked_ during construction of the object.

    Given this situation, you'd be better off using _placement_new_
    (read about it in a good C++ book).
    [color=blue]
    > //
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > class X {
    > int Data;
    > public:
    > X() { Data = 56; };
    > X(const X& Y) { Data = Y.Data; }; // Copy Constructor
    > void Construct(const X& Y) { Data = Y.Data; };
    > };
    >
    > int main()
    > {
    > X ABC;
    > X* Temp = (X*) malloc(sizeof(X ) * 10);
    > for (int N = 0; N < 10; N++) {
    > // Temp[N].(ABC); // Does Not Compile
    > // Temp[N](ABC); // Does Not Compile
    > Temp[N].Construct(ABC) ; // Compiles and Executes Correctly
    > }
    > free(Temp);
    > return 0;
    > }[/color]

    V


    Comment

    • Jonathan Turkanis

      #3
      Re: Copy Constructor for an Array Element ???


      "Victor Bazarov" <v.Abazarov@com Acast.net> wrote in message:[color=blue]
      > "Peter Olcott" <olcott@worldne t.att.net> wrote...[color=green]
      > > //
      > > // Is there something wrong with my syntax for the
      > > // Copy Constructor of an Array Element, or does
      > > // the C++ language not support this?[/color]
      >
      > The latter. Constructors do not have names, therefore they cannot
      > be found during name lookup and hence cannot be _called_. They can
      > only be _invoked_ during construction of the object.
      >
      > Given this situation, you'd be better off using _placement_new_
      > (read about it in a good C++ book).[/color]

      I'd recommend using an assignment operator or a member function assign
      (like std library containers) instead of placement new, unless you are
      absolutely sure you need it.

      And if you do use placement new to constuct an object on top of an
      existing object, make sure to call the objects destructor first
      (unless you are absolutely sure it's not necessary.)

      Jonathan


      Comment

      • E. Robert Tisdale

        #4
        Re: Copy Constructor for an Array Element ???

        Peter Olcott wrote:
        [color=blue]
        > //
        > // Is there something wrong with my syntax for the
        > // Copy Constructor of an Array Element, or does
        > // the C++ language not support this?
        > //
        >
        > #include <stdio.h>
        > #include <stdlib.h>
        >
        > class X {[/color]
        private:
        // representation[color=blue]
        > int Data;
        > public:
        > X(void): Data(56) { } // default
        > X(const X& Y): Data(Y.Data) { } // Copy Constructor
        > // void Construct(const X& Y) { Data = Y.Data; };[/color]
        // too late. *this already constructed
        X& modify(const X& Y) { Data = Y.Data; return *this; }[color=blue]
        > };
        >
        > int main(int argc, char* argv[]) {
        > X ABC;
        > X* Temp = (X*)malloc(size of(X)*10); // you shouldn't do this
        > for (int N = 0; N < 10; ++N) {[/color]
        Temp[N] = ABC; // uses default assignment
        Temp[N].modify(ABC);[color=blue]
        > }
        > free(Temp);
        > return 0;
        > }[/color]

        Comment

        • Peter Olcott

          #5
          Re: Copy Constructor for an Array Element ???

          > I'd recommend using an assignment operator or a member function assign[color=blue]
          > (like std library containers) instead of placement new, unless you are
          > absolutely sure you need it.[/color]

          I just created a std::vector class for antique (pre-template) C++ compilers.
          My intention was to implement this within minimal execution time.
          Apparently, it seems that even the modern std::vectors must use a kludge
          to initialize their members. They seem to require default construction and
          then assignment, rather than the single integrated step of copy construction.
          Hopefully this shortcoming will by addressed in the continuing evolution
          of C++.
          [color=blue]
          > And if you do use placement new to constuct an object on top of an
          > existing object, make sure to call the objects destructor first
          > (unless you are absolutely sure it's not necessary.)
          >
          > Jonathan
          >
          >[/color]


          Comment

          • Cy Edmunds

            #6
            Re: Copy Constructor for an Array Element ???

            "Peter Olcott" <olcott@worldne t.att.net> wrote in message
            news:ZPd%b.9482 9$hR.1884319@bg tnsc05-news.ops.worldn et.att.net...[color=blue][color=green]
            > > I'd recommend using an assignment operator or a member function assign
            > > (like std library containers) instead of placement new, unless you are
            > > absolutely sure you need it.[/color]
            >
            > I just created a std::vector class for antique (pre-template) C++[/color]
            compilers.[color=blue]
            > My intention was to implement this within minimal execution time.
            > Apparently, it seems that even the modern std::vectors must use a kludge
            > to initialize their members. They seem to require default construction and
            > then assignment, rather than the single integrated step of copy[/color]
            construction.

            [snip]

            Well, not really:

            std::vector<int > a;
            a.reserve(3);
            a.push_back(2);
            a.push_back(4);
            a.push_back(-2);

            reserve just allocates enough space and a copy constructor is used for each
            element. No default constructor and no assignment. The only inefficiency
            that I know of is that the number of push_back's is counted.


            Comment

            • Peter Olcott

              #7
              Re: Copy Constructor for an Array Element ???

              > > class X {[color=blue]
              > private:
              > // representation[color=green]
              > > int Data;
              > > public:
              > > X(void): Data(56) { } // default
              > > X(const X& Y): Data(Y.Data) { } // Copy Constructor
              > > // void Construct(const X& Y) { Data = Y.Data; };[/color]
              > // too late. *this already constructed[/color]

              I don't think so. I think that the example might be the only
              way to copy construct array elements.
              [color=blue]
              > X& modify(const X& Y) { Data = Y.Data; return *this; }[color=green]
              > > };
              > >
              > > int main(int argc, char* argv[]) {
              > > X ABC;
              > > X* Temp = (X*)malloc(size of(X)*10); // you shouldn't do this
              > > for (int N = 0; N < 10; ++N) {[/color]
              > Temp[N] = ABC; // uses default assignment
              > Temp[N].modify(ABC);[color=green]
              > > }
              > > free(Temp);
              > > return 0;
              > > }[/color]
              >[/color]


              Comment

              • Peter Olcott

                #8
                Re: Copy Constructor for an Array Element ???

                > std::vector<int > a;[color=blue]
                > a.reserve(3);
                > a.push_back(2);
                > a.push_back(4);
                > a.push_back(-2);
                >
                > reserve just allocates enough space and a copy constructor is used for each
                > element. No default constructor and no assignment. The only inefficiency
                > that I know of is that the number of push_back's is counted.
                >[/color]
                Try the same sort of example with a class that does memory allocation,
                such that a deep copy (rather than bitwise) is required.



                Comment

                • Jonathan Turkanis

                  #9
                  Re: Copy Constructor for an Array Element ???


                  "Peter Olcott" <olcott@worldne t.att.net> wrote in message
                  news:nfe%b.9496 6$hR.1884982@bg tnsc05-news.ops.worldn et.att.net...[color=blue][color=green]
                  > > std::vector<int > a;
                  > > a.reserve(3);
                  > > a.push_back(2);
                  > > a.push_back(4);
                  > > a.push_back(-2);
                  > >
                  > > reserve just allocates enough space and a copy constructor is used[/color][/color]
                  for each[color=blue][color=green]
                  > > element. No default constructor and no assignment. The only[/color][/color]
                  inefficiency[color=blue][color=green]
                  > > that I know of is that the number of push_back's is counted.
                  > >[/color]
                  > Try the same sort of example with a class that does memory[/color]
                  allocation,[color=blue]
                  > such that a deep copy (rather than bitwise) is required.
                  >[/color]

                  Cy is correct.A vector obtains unitialized memory from an allocator,
                  and initilizes each element, when appropriate, then initializes them
                  using copy constructors with placement new. A default constructor is
                  not even required to exist. (See 20.1.4).

                  Of course, if the copy constructor is expensive, then push_back will
                  be too. There's no way to avoid this.

                  Jonathan


                  Comment

                  • Jonathan Turkanis

                    #10
                    Re: Copy Constructor for an Array Element ???


                    "Jonathan Turkanis" <technews@kanga roologic.com> wrote in message
                    news:c1js4e$1jj jur$1@ID-216073.news.uni-berlin.de...[color=blue]
                    >[/color]
                    [color=blue]
                    >
                    > Cy is correct.A vector obtains unitialized memory from an allocator,
                    > and initilizes each element, when appropriate, then initializes them
                    > using copy constructors with placement new. A default constructor is[/color]

                    This came out garbled. It should say:

                    A vector obtains unitialized memory from an allocator, then initilizes
                    each element, when appropriate, using copy constructors with placement
                    new

                    Jonathan


                    Comment

                    • tom_usenet

                      #11
                      Re: Copy Constructor for an Array Element ???

                      On Thu, 26 Feb 2004 01:49:24 GMT, "Peter Olcott"
                      <olcott@worldne t.att.net> wrote:
                      [color=blue]
                      >//
                      >// Is there something wrong with my syntax for the
                      >// Copy Constructor of an Array Element, or does
                      >// the C++ language not support this?[/color]

                      It does, via "placement new". See below.
                      [color=blue]
                      >//
                      >
                      >#include <stdio.h>
                      >#include <stdlib.h>
                      >
                      >class X {
                      >int Data;
                      >public:
                      > X() { Data = 56; };
                      > X(const X& Y) { Data = Y.Data; }; // Copy Constructor
                      > void Construct(const X& Y) { Data = Y.Data; };
                      >};
                      >
                      >int main()
                      >{
                      >X ABC;
                      >X* Temp = (X*) malloc(sizeof(X ) * 10);
                      > for (int N = 0; N < 10; N++) {
                      >// Temp[N].(ABC); // Does Not Compile
                      >// Temp[N](ABC); // Does Not Compile
                      > Temp[N].Construct(ABC) ; // Compiles and Executes Correctly[/color]

                      new (Temp + N) X(ABC); //placement construction
                      [color=blue]
                      > }
                      > free(Temp);
                      >return 0;
                      >}[/color]

                      Tom
                      --
                      C++ FAQ: http://www.parashift.com/c++-faq-lite/
                      C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

                      Comment

                      • Peter Olcott

                        #12
                        Re: Copy Constructor for an Array Element ???

                        > > Cy is correct.A vector obtains unitialized memory from an allocator,[color=blue][color=green]
                        > > and initilizes each element, when appropriate, then initializes them
                        > > using copy constructors with placement new. A default constructor is[/color]
                        >
                        > This came out garbled. It should say:
                        >
                        > A vector obtains unitialized memory from an allocator, then initilizes
                        > each element, when appropriate, using copy constructors with placement
                        > new
                        >
                        > Jonathan
                        >[/color]
                        I placed messages in every constructor and destructor of my class,
                        and had std::vector resize() itself. In both of my compilers the number
                        of elements constructed was one more than I asked for. This seems to
                        indicate that the compiler is somehow created an extra temporary
                        object.

                        I will look into this <placement new> more deeply, though.


                        Comment

                        • Peter Olcott

                          #13
                          Re: Copy Constructor for an Array Element ???


                          "tom_usenet " <tom_usenet@hot mail.com> wrote in message
                          news:44nr30tkr9 uu6s52iba1j45kq rahl839cb@4ax.c om...[color=blue]
                          > On Thu, 26 Feb 2004 01:49:24 GMT, "Peter Olcott"
                          > <olcott@worldne t.att.net> wrote:
                          >[color=green]
                          > >//
                          > >// Is there something wrong with my syntax for the
                          > >// Copy Constructor of an Array Element, or does
                          > >// the C++ language not support this?[/color]
                          >
                          > It does, via "placement new". See below.
                          >[color=green]
                          > >//
                          > >
                          > >#include <stdio.h>
                          > >#include <stdlib.h>
                          > >
                          > >class X {
                          > >int Data;
                          > >public:
                          > > X() { Data = 56; };
                          > > X(const X& Y) { Data = Y.Data; }; // Copy Constructor
                          > > void Construct(const X& Y) { Data = Y.Data; };
                          > >};
                          > >
                          > >int main()
                          > >{
                          > >X ABC;
                          > >X* Temp = (X*) malloc(sizeof(X ) * 10);
                          > > for (int N = 0; N < 10; N++) {
                          > >// Temp[N].(ABC); // Does Not Compile
                          > >// Temp[N](ABC); // Does Not Compile
                          > > Temp[N].Construct(ABC) ; // Compiles and Executes Correctly[/color]
                          >
                          > new (Temp + N) X(ABC); //placement construction[/color]

                          None of my three compilers would accept this.
                          [color=blue][color=green]
                          > > }
                          > > free(Temp);
                          > >return 0;
                          > >}[/color]
                          >
                          > Tom
                          > --
                          > C++ FAQ: http://www.parashift.com/c++-faq-lite/
                          > C FAQ: http://www.eskimo.com/~scs/C-faq/top.html[/color]


                          Comment

                          • tom_usenet

                            #14
                            Re: Copy Constructor for an Array Element ???

                            On Thu, 26 Feb 2004 13:02:27 GMT, "Peter Olcott"
                            <olcott@worldne t.att.net> wrote:
                            [color=blue][color=green]
                            >> new (Temp + N) X(ABC); //placement construction[/color]
                            >
                            >None of my three compilers would accept this.[/color]

                            What was the error?

                            You need #include <new> at the top (or possibly <new.h> for
                            pre-standard compilers). Then it should be accepted by any recent
                            compiler, and certainly MSVC6+, GCC 2.95+, etc.

                            Tom
                            --
                            C++ FAQ: http://www.parashift.com/c++-faq-lite/
                            C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

                            Comment

                            • Old Wolf

                              #15
                              Re: Copy Constructor for an Array Element ???

                              "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message news:<403D57CE. 50700@jpl.nasa. gov>...[color=blue]
                              > Peter Olcott wrote:[/color]

                              Of the 21 lines in Peter Olcott's posted program, only 6 were quoted
                              unmodified by Trollsdale.

                              If we ignore whitespace modifications, 10 lines were quoted unmodified
                              (less than half of the posted program). Is this a record?

                              Not to mention the fact that all of the supplied
                              modifications are wrong or irrelevant, and not helpful.
                              [color=blue]
                              >[color=green]
                              > > //
                              > > // Is there something wrong with my syntax for the
                              > > // Copy Constructor of an Array Element, or does
                              > > // the C++ language not support this?
                              > > //
                              > >
                              > > #include <stdio.h>
                              > > #include <stdlib.h>
                              > >
                              > > class X {[/color]
                              > private:
                              > // representation[color=green]
                              > > int Data;
                              > > public:
                              > > X(void): Data(56) { } // default
                              > > X(const X& Y): Data(Y.Data) { } // Copy Constructor
                              > > // void Construct(const X& Y) { Data = Y.Data; };[/color]
                              > // too late. *this already constructed
                              > X& modify(const X& Y) { Data = Y.Data; return *this; }[color=green]
                              > > };
                              > >
                              > > int main(int argc, char* argv[]) {
                              > > X ABC;
                              > > X* Temp = (X*)malloc(size of(X)*10); // you shouldn't do this
                              > > for (int N = 0; N < 10; ++N) {[/color]
                              > Temp[N] = ABC; // uses default assignment
                              > Temp[N].modify(ABC);[color=green]
                              > > }
                              > > free(Temp);
                              > > return 0;
                              > > }[/color][/color]

                              Comment

                              Working...