C++: Default Copy Constructor

Discussion in 'C++' started by A, Nov 20, 2003.

  1. A

    A Guest

    Hi,

    A default copy constructor is created for you when you don't specify one
    yourself. In such case, the default copy constructor will simply do a
    bitwise copy for primitives (including pointers) and for objects types call
    their default constructor.

    Any others points i should know?


    Regards,
    A
     
    A, Nov 20, 2003
    #1
    1. Advertising

  2. A wrote:
    >
    > Hi,
    >
    > A default copy constructor is created for you when you don't specify one
    > yourself. In such case, the default copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types call
    > their default constructor.


    That would be silly.
    Of course it uses the copy constructor for object types.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Nov 20, 2003
    #2
    1. Advertising

  3. Re: Default Copy Constructor

    > A default copy constructor is created for you when you don't specify one
    > yourself. In such case, the default copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types

    call
    > their default constructor.


    Why think of it that way? Aside from it being incorrect, I mean.

    If you got at explanation from a book, I suggest you avoid that book in the
    future.

    The default copy constructor for a class copies the (non-static) data
    members of the class. Exactly what it means to copy the member depends on
    the member's type, just as the meaning of copying any object depends on the
    object's type.
     
    Andrew Koenig, Nov 20, 2003
    #3
  4. A

    Ron Natalie Guest

    Re: Default Copy Constructor

    "A" <> wrote in message news:...
    > Hi,
    >
    > A default copy constructor is created for you when you don't specify one
    > yourself. In such case, the default copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types call
    > their default constructor.


    Default copy constructor is a bit confusing terminology. I prefer "implicitly
    defined" or "compiler generated' constructor.

    The implicitly defined copy constructor invokes the copy constructor (not
    the default) to copy all the subobjects.
     
    Ron Natalie, Nov 20, 2003
    #4
  5. A

    Gavin Deane Guest

    "A" <> wrote in message news:<>...
    > Hi,


    Hello

    > A default copy constructor is created for you when you don't specify one
    > yourself. In such case, the default copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types call
    > their default constructor.


    There is no such thing as a default copy constructor. There are
    default constructors and copy constructors and they are different
    things.

    The implicitly defined copy constructor (which I think is what you
    mean by "default copy constructor") will copy non-static members of
    class type using their copy constructor, not their default
    constructor. The implicitly defined copy constructor is used when you
    don't define your own copy constructor.

    > Any others points i should know?


    Undoubtedly :) But it would help if you were a bit more specific. Have
    you come across some code you can't figure out, or a passage in a text
    book you don't understand?

    GJD
     
    Gavin Deane, Nov 20, 2003
    #5
  6. A

    osmium Guest

    Re: Default Copy Constructor

    Andrew Koenig writes:

    > > A default copy constructor is created for you when you don't specify one
    > > yourself. In such case, the default copy constructor will simply do a
    > > bitwise copy for primitives (including pointers) and for objects types

    > call
    > > their default constructor.

    >
    > Why think of it that way? Aside from it being incorrect, I mean.
    >
    > If you got at explanation from a book, I suggest you avoid that book in

    the
    > future.


    I rarely pile on during "bad book" crusades. But I will make an exception
    for that book. Sometimes there is an innocuous statement that is wrong in a
    pedant's eyes but harmless; there is no way I could construe that as
    harmless.
     
    osmium, Nov 20, 2003
    #6
  7. A

    Joe Hesse Guest

    Re: Default Copy Constructor

    Why isn't the copy for primitives called a bytewise copy instead of a
    bitwise copy?
    Joe Hesse

    "A" <> wrote in message
    news:...
    > Hi,
    >
    > A default copy constructor is created for you when you don't specify one
    > yourself. In such case, the default copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types

    call
    > their default constructor.
    >
    > Any others points i should know?
    >
    >
    > Regards,
    > A
    >
    >
    >
     
    Joe Hesse, Nov 20, 2003
    #7
  8. Re: Default Copy Constructor

    "Joe Hesse" <> wrote...
    > Why isn't the copy for primitives called a bytewise copy instead of a
    > bitwise copy?


    If by "primitives" you (and "A") mean "members", then it's called
    "member-wise" copy. Every member is copied using its copy c-tor
    (and if it's a built-in type, like a pointer), using the built-in
    mechanism of copy-initialisation.

    > Joe Hesse
    >
    > "A" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > A default copy constructor is created for you when you don't specify one
    > > yourself. In such case, the default copy constructor will simply do a
    > > bitwise copy for primitives (including pointers) and for objects types

    > call
    > > their default constructor.
    > >
    > > Any others points i should know?
    > >
    > >
    > > Regards,
    > > A
    > >
    > >
    > >

    >
    >
     
    Victor Bazarov, Nov 20, 2003
    #8
  9. A

    Ron Natalie Guest

    Re: Default Copy Constructor

    "Joe Hesse" <> wrote in message news:3fbcf561$0$41293$...
    > Why isn't the copy for primitives called a bytewise copy instead of a
    > bitwise copy?
    >

    I'm not sure why it's called either one. Each subobject is copied according
    to its own copy semantics, be it via copy constructor or by some internal
    means. Most implementations copy things in bigger increments than bits
    OR bytes when necesssary.
     
    Ron Natalie, Nov 20, 2003
    #9
  10. A

    A Guest

    Re: Default Copy Constructor


    > Hi,
    >
    > A default copy constructor is created for you when you don't specify one
    > yourself. In such case, the default copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types

    call
    > their default constructor.
    >
    > Any others points i should know?
    >
    >
    > Regards,
    > A


    Firstly, i would like to point out that a default copy constructor is just
    another name for the implicitly defined copy constructor that is created for
    you when you do not explicitly specify one yourself.

    It seems that I have confused a few people with my inaccurate statement, so
    I will make the following amendments:

    A default copy constructor is created for you when you don't specify one
    yourself. In such case, the default copy constructor will simply do a
    bitwise copy for primitives (including pointers) and for objects types call
    their copy constructor. If in the later case, a copy constructor is not
    explicitly defined then, in turn, a default copy constructor will be
    implicitly created.

    I believe this is ROCK SOLID now!


    Regards,
    A





    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.538 / Virus Database: 333 - Release Date: 10/11/2003
     
    A, Nov 21, 2003
    #10
  11. Re: Default Copy Constructor

    A wrote:
    >
    > > Hi,
    > >
    > > A default copy constructor is created for you when you don't specify one
    > > yourself. In such case, the default copy constructor will simply do a
    > > bitwise copy for primitives (including pointers) and for objects types

    > call
    > > their default constructor.
    > >
    > > Any others points i should know?
    > >
    > >
    > > Regards,
    > > A

    >
    > Firstly, i would like to point out that a default copy constructor is just
    > another name for the implicitly defined copy constructor that is created for
    > you when you do not explicitly specify one yourself.
    >
    > It seems that I have confused a few people with my inaccurate statement, so
    > I will make the following amendments:
    >
    > A default copy constructor is created for you when you don't specify one
    > yourself. In such case, the default copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types call
    > their copy constructor. If in the later case, a copy constructor is not
    > explicitly defined then, in turn, a default copy constructor will be
    > implicitly created.
    >
    > I believe this is ROCK SOLID now!


    not really.
    It is not consistent with the use of the word 'default' in conjunction
    with the word 'constructor'.

    A default constructor is a constructor which can be called without
    specifying arguments.

    Thus

    C::C(); is a default constructor
    C::C( int i = 0 ); is a default constructor
    C::C( in j ); is *not* a default constructor, because it can only
    be used if some argument is specified.

    So the meaning of 'default' in the context of constructors has nothing to do
    with who created the function, either the programmer or the compiler. It has
    to do with beeing able to be called without arguments.

    That's why there is no 'default copy constructor', because a copy constructor
    per definition has to be called with an argument: the object to make a copy from.

    Also: replace the action a compiler generated copy constructor does with:

    ... the compiler generated copy constructor does a member wise copy of
    its members. For builtin types (including pointers) this means a bitwise
    copy. For other types the copy constructor of the member is used.

    It is not the constructor which decides how this copy should be done. It
    is the member which decides this.

    Drop the last sentence ( "If in the later case, .... " ), it is not needed
    and follows from the beginning of your paragraph ( "A default copy constructor
    is cretaed for you when ..." ).
    The compiler will always generate a cctor if none is specified, if it
    needs one. This does include but is not restricted to: if used in another
    constructor.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Nov 21, 2003
    #11
  12. A

    Gavin Deane Guest

    Re: Default Copy Constructor

    "A" <> wrote in message news:<>...
    > > Hi,
    > >
    > > A default copy constructor is created for you when you don't specify one
    > > yourself. In such case, the default copy constructor will simply do a
    > > bitwise copy for primitives (including pointers) and for objects types

    > call
    > > their default constructor.
    > >
    > > Any others points i should know?
    > >
    > >
    > > Regards,
    > > A

    >
    > Firstly, i would like to point out that a default copy constructor is just
    > another name for the implicitly defined copy constructor that is created for
    > you when you do not explicitly specify one yourself.


    It's not a very good alternative name. Where did you get it from?

    > It seems that I have confused a few people with my inaccurate statement, so
    > I will make the following amendments:


    You run the risk of continuing to confuse people if you don't change
    the phrase "default copy constructor"

    > A default copy constructor is created for you when you don't specify one
    > yourself. In such case, the default copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types call
    > their copy constructor. If in the later case, a copy constructor is not
    > explicitly defined then, in turn, a default copy constructor will be
    > implicitly created.


    Better than the original, but Karl Heinz Buchegger's points are well
    worth taking note of.

    GJD
     
    Gavin Deane, Nov 21, 2003
    #12
  13. A

    A Guest

    Re: Default Copy Constructor


    > > Hi,
    > >
    > > A default copy constructor is created for you when you don't specify one
    > > yourself. In such case, the default copy constructor will simply do a
    > > bitwise copy for primitives (including pointers) and for objects types

    > call
    > > their default constructor.
    > >
    > > Any others points i should know?
    > >
    > >
    > > Regards,
    > > A

    >
    > Firstly, i would like to point out that a default copy constructor is just
    > another name for the implicitly defined copy constructor that is created

    for
    > you when you do not explicitly specify one yourself.
    >
    > It seems that I have confused a few people with my inaccurate statement,

    so
    > I will make the following amendments:
    >
    > A default copy constructor is created for you when you don't specify one
    > yourself. In such case, the default copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types

    call
    > their copy constructor. If in the later case, a copy constructor is not
    > explicitly defined then, in turn, a default copy constructor will be
    > implicitly created.
    >
    > I believe this is ROCK SOLID now!


    After further reading, it seems I may have still confused people. Let me
    clarify once again.

    Edit: An implicit copy constructor is created for you when you don't specify
    one yourself. In such case, the implicit copy constructor will simply do a
    bitwise copy for primitives (including pointers) and for objects types call
    their copy constructor. If in the later case, a copy constructor is not
    explicitly defined then, in turn, an implicit copy constructor will be
    created.


    Regards,
    A
     
    A, Nov 22, 2003
    #13
  14. A

    Gavin Deane Guest

    Re: Default Copy Constructor

    "A" <> wrote in message news:<3fbecf38$>...

    <snip>

    > After further reading, it seems I may have still confused people. Let me
    > clarify once again.
    >
    > Edit: An implicit copy constructor is created for you when you don't specify
    > one yourself. In such case, the implicit copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types call
    > their copy constructor. If in the later case, a copy constructor is not
    > explicitly defined then, in turn, an implicit copy constructor will be
    > created.


    I think that's cleared up the confusion. The last sentence isn't
    strictly necessary as it logically follows from the first sentence.
    But it does no harm and may help to clarify things for the reader.

    There is just one technical nit to pick though (not related to the
    original confusion). In the last sentence, change "defined" to
    "declared". You could change "specify" to "declare" in the first
    sentence too.

    Whether the compiler is allowed use the implicit copy constructor
    depends on whether you have _declared_ one yourself, not on whether
    you have _defined_ one. So this class

    class C
    {
    public:
    C() {}

    private:
    C(const C& rhs); // no definition provided for
    // this copy ctor.
    };

    can not be copied. A copy constructor is explicitly declared (though
    it's not defined anywhere) so when the compiler encounters

    void f()
    {
    C c1; // OK, used default constructor
    C c2(c1); // Error
    }

    it can not use the implicit copy constructor on the error line because
    there is a copy constructor declared in the class

    hth ;-)
    GJD
     
    Gavin Deane, Nov 22, 2003
    #14
  15. A

    A Guest

    Re: Default Copy Constructor


    > > Edit: An implicit copy constructor is created for you when you don't

    specify
    > > one yourself. In such case, the implicit copy constructor will simply do

    a
    > > bitwise copy for primitives (including pointers) and for objects types

    call
    > > their copy constructor. If in the later case, a copy constructor is not
    > > explicitly defined then, in turn, an implicit copy constructor will be
    > > created.

    >
    > I think that's cleared up the confusion. The last sentence isn't
    > strictly necessary as it logically follows from the first sentence.
    > But it does no harm and may help to clarify things for the reader.
    >
    > There is just one technical nit to pick though (not related to the
    > original confusion). In the last sentence, change "defined" to
    > "declared". You could change "specify" to "declare" in the first
    > sentence too.
    >
    > Whether the compiler is allowed use the implicit copy constructor
    > depends on whether you have _declared_ one yourself, not on whether
    > you have _defined_ one. So this class
    >
    > class C
    > {
    > public:
    > C() {}
    >
    > private:
    > C(const C& rhs); // no definition provided for
    > // this copy ctor.
    > };
    >
    > can not be copied. A copy constructor is explicitly declared (though
    > it's not defined anywhere) so when the compiler encounters
    >
    > void f()
    > {
    > C c1; // OK, used default constructor
    > C c2(c1); // Error
    > }
    >
    > it can not use the implicit copy constructor on the error line because
    > there is a copy constructor declared in the class
    >
    > hth ;-)
    > GJD


    point taken.

    Edit: An implicit copy constructor is created for you when you don't declare
    one yourself. In such case, the implicit copy constructor will simply do a
    bitwise copy for primitives (including pointers) and for objects types call
    their copy constructor. If in the later case, a copy constructor is not
    explicitly declared then, in turn, an implicit copy constructor will be
    created.

    Regards,
    A


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.538 / Virus Database: 333 - Release Date: 10/11/2003
     
    A, Nov 23, 2003
    #15
  16. A

    Gavin Deane Guest

    Re: Default Copy Constructor

    "A" <> wrote in message news:<>...
    > > > Edit: An implicit copy constructor is created for you when you don't

    > specify
    > > > one yourself. In such case, the implicit copy constructor will simply do

    > a
    > > > bitwise copy for primitives (including pointers) and for objects types

    > call
    > > > their copy constructor. If in the later case, a copy constructor is not
    > > > explicitly defined then, in turn, an implicit copy constructor will be
    > > > created.

    > >
    > > I think that's cleared up the confusion. The last sentence isn't
    > > strictly necessary as it logically follows from the first sentence.
    > > But it does no harm and may help to clarify things for the reader.
    > >
    > > There is just one technical nit to pick though (not related to the
    > > original confusion). In the last sentence, change "defined" to
    > > "declared". You could change "specify" to "declare" in the first
    > > sentence too.
    > >
    > > Whether the compiler is allowed use the implicit copy constructor
    > > depends on whether you have _declared_ one yourself, not on whether
    > > you have _defined_ one.


    <snip>

    > point taken.
    >
    > Edit: An implicit copy constructor is created for you when you don't declare
    > one yourself. In such case, the implicit copy constructor will simply do a
    > bitwise copy for primitives (including pointers) and for objects types call
    > their copy constructor. If in the later case, a copy constructor is not
    > explicitly declared then, in turn, an implicit copy constructor will be
    > created.
    >
    > Regards,
    > A


    I'd go with that.

    GJD
     
    Gavin Deane, Nov 23, 2003
    #16
    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. Aire
    Replies:
    3
    Views:
    476
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    9
    Views:
    984
    Alf P. Steinbach
    Mar 6, 2006
  3. ali
    Replies:
    4
    Views:
    592
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,294
  5. cinsk
    Replies:
    35
    Views:
    2,662
    James Kanze
    Oct 11, 2010
Loading...

Share This Page