copy constructor

Discussion in 'C++' started by pankajrg, Oct 30, 2006.

  1. pankajrg

    pankajrg Guest

    why there is a need of eplicitly defining the copy constructor.
    please correct me, Is it true that copy constructor are created by
    compiler, by default
    if we don't create copy constructor.
    pankajrg, Oct 30, 2006
    #1
    1. Advertising

  2. pankajrg wrote:
    > why there is a need of eplicitly defining the copy constructor.


    Did you lose "I don't see" at the beginning of this sentence?

    > please correct me, Is it true that copy constructor are created by
    > compiler, by default
    > if we don't create copy constructor.


    Yes, they are. The need to have one exists if you don't want the
    default behaviour, which is to copy-construct every member.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 30, 2006
    #2
    1. Advertising

  3. pankajrg

    Daniel T. Guest

    "pankajrg" <> wrote:

    > why there is a need of eplicitly defining the copy constructor.
    > please correct me, Is it true that copy constructor are created by
    > compiler, by default
    > if we don't create copy constructor.


    If the compiler supplied copy constructor doesn't do what you want, then
    you have to provide one yourself.

    http://www.parashift.com/c -faq-lite/coding-standards.html#faq-27.10

    A class with any of {destructor, assignment operator, copy constructor}
    generally needs all 3

    --
    To send me email, put "sheltie" in the subject.
    Daniel T., Oct 30, 2006
    #3
  4. pankajrg

    Marcus Kwok Guest

    Daniel T. <> wrote:
    > A class with any of {destructor, assignment operator, copy constructor}
    > generally needs all 3


    Someone recently showed me this article, saying that using modern
    techniques, one of the three should be a non-issue:

    "The Law of The Big Two"
    by Bjorn Karlsson and Matthew Wilson
    http://www.artima.com/cppsource/bigtwo.html

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Oct 30, 2006
    #4
  5. pankajrg

    Salt_Peter Guest

    pankajrg wrote:
    > why there is a need of eplicitly defining the copy constructor.
    > please correct me, Is it true that copy constructor are created by
    > compiler, by default
    > if we don't create copy constructor.


    Yes, the copy ctor is indeed generated for you. Often enough, but not
    always, is that acceptable.

    Lets put it this way.
    You want to copy an exact replica of the house your neighbour has. You
    love that house so much you want every detail to be exactly the same.
    So you let a default copy constructor replicate the new home for you -
    bit by bit, brick by brick. Great! its a new object so it gets its own
    mailing address automatically.

    Unfortunately, you are receiving some of your neighbour's mail and none
    of your own. Strangely enough, you notice that the address posted on
    your front porch is not correct (it shows your neighbour's address!
    What the hell?).

    How do you prevent that situation? What if you have 10,000 of these
    houses to get copied?
    Salt_Peter, Oct 30, 2006
    #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,893
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    463
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    572
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,221
  5. cinsk
    Replies:
    35
    Views:
    2,600
    James Kanze
    Oct 11, 2010
Loading...

Share This Page