Copy constructor

Discussion in 'C++' started by Jakob Bieling, Aug 29, 2003.

  1. "Terry" <> wrote in message
    news:3f4f56f8$...
    > Hi,
    >
    > Can someone please explain what a copy constructor does and how it's
    > useful? Thanks,



    It constructs an object (like the regular c'tor) from an already
    existing object. This way you avoid first constructing and then assigning
    (ie. two steps, instead of one).

    hth
    --
    jb

    (replace y with x if you want to reply by e-mail)
    Jakob Bieling, Aug 29, 2003
    #1
    1. Advertising

  2. Jakob Bieling

    Terry Guest

    Hi,

    Can someone please explain what a copy constructor does and how it's
    useful? Thanks,

    Terry
    Terry, Aug 29, 2003
    #2
    1. Advertising

  3. Jakob Bieling

    A Guest


    > > Hi,
    > >
    > > Can someone please explain what a copy constructor does and how it's
    > > useful? Thanks,

    >
    >
    > It constructs an object (like the regular c'tor) from an already
    > existing object. This way you avoid first constructing and then assigning
    > (ie. two steps, instead of one).


    can't you use use the assignment operator to copy? that should be one step
    as well.

    asasas
    A, Aug 30, 2003
    #3
  4. "A" <> wrote...
    >
    >
    > > > Hi,
    > > >
    > > > Can someone please explain what a copy constructor does and how it's
    > > > useful? Thanks,

    > >
    > >
    > > It constructs an object (like the regular c'tor) from an already
    > > existing object. This way you avoid first constructing and then

    assigning
    > > (ie. two steps, instead of one).

    >
    > can't you use use the assignment operator to copy? that should be one step
    > as well.


    No. Assignment operator is useless when passing parameters by
    value or returning by value is concerned. Those are the main
    uses of the copy constructor.

    Victor
    Victor Bazarov, Aug 30, 2003
    #4
  5. "A" <> wrote in message
    news:...
    >
    >
    > > > Hi,
    > > >
    > > > Can someone please explain what a copy constructor does and how it's
    > > > useful? Thanks,

    > >
    > >
    > > It constructs an object (like the regular c'tor) from an already
    > > existing object. This way you avoid first constructing and then

    assigning
    > > (ie. two steps, instead of one).

    >
    > can't you use use the assignment operator to copy? that should be one step
    > as well.
    >
    > asasas
    >


    A a;
    A b(a); // copy construct, one step
    A c; // default construct, step 1
    c = a; // assign, step 2

    Creating a copy of an object using assignment uses two steps, first the
    object is constructed, then it is assigned.

    john
    John Harrison, Aug 30, 2003
    #5
    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. VisionSet
    Replies:
    8
    Views:
    4,864
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    450
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    554
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,184
  5. cinsk
    Replies:
    35
    Views:
    2,542
    James Kanze
    Oct 11, 2010
Loading...

Share This Page