Questions of copy constructor

Discussion in 'C++' started by away, Sep 14, 2004.

  1. away

    away Guest

    1. When a class defined with a member of pointer type, it's necessary to
    have a copy constructor and assignment operator.

    If don't pass objects of such class as value in a function and don't do
    assignment, should copy constructor and assignment operator be unnecessary?

    2. If a base class have a pointer member, should its derived classes also
    need copy constructor and assignment operator, no matter if a derived class
    itself has a pointer member or not?

    3. If such a pointer member of pointer type of "void", such void type can be
    casted to any other type at runtime, how to code a copy constructor for such
    a member?

    Thanks for help!
     
    away, Sep 14, 2004
    #1
    1. Advertising

  2. away wrote:
    > 1. When a class defined with a member of pointer type, it's necessary to
    > have a copy constructor and assignment operator.


    Depends on what the pointer points to.

    > If don't pass objects of such class as value in a function and don't do
    > assignment, should copy constructor and assignment operator be unnecessary?


    Depends on existence of other operations that may require copy
    construction (like storing in a standard container).

    > 2. If a base class have a pointer member, should its derived classes also
    > need copy constructor and assignment operator, no matter if a derived class
    > itself has a pointer member or not?


    Probably not, depends on the derived class.

    > 3. If such a pointer member of pointer type of "void", such void type can be
    > casted to any other type at runtime, how to code a copy constructor for such
    > a member?


    One should never use such mechanism. Rethink your design.

    V
     
    Victor Bazarov, Sep 14, 2004
    #2
    1. Advertising

  3. away wrote:
    >
    > 1. When a class defined with a member of pointer type, it's necessary to
    > have a copy constructor and assignment operator.


    Depends if this pointer is an owning pointer or not.

    >
    > If don't pass objects of such class as value in a function and don't do
    > assignment, should copy constructor and assignment operator be unnecessary?


    No. But you could do:
    Declare the copy constructor and assignement operator private and don't implement
    them. Then the compiler and/or linker will guard you if you make the mistake of
    violating your rule.

    >
    > 2. If a base class have a pointer member, should its derived classes also
    > need copy constructor and assignment operator, no matter if a derived class
    > itself has a pointer member or not?


    If the derived class on its own doesn't need a copy constructor/op= then
    no. You don't have to do anything. The compiler generated ones will do
    the right thing.

    >
    > 3. If such a pointer member of pointer type of "void", such void type can be
    > casted to any other type at runtime, how to code a copy constructor for such
    > a member?


    Bad idea. You shouldn't do this in the first place.


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Sep 14, 2004
    #3
  4. "away" <> wrote in message
    news:B2D1d.364613$...
    > 1. When a class defined with a member of pointer type, it's necessary to
    > have a copy constructor and assignment operator.
    >


    Not always true. It's only necessary to have a copy constructor if the
    compiler generated one does the wrong thing. Having a pointer doesn't
    necessarily mean the compiler generated copy constructor is wrong.

    A better rule is the rule of three. If your class has a destructor or a copy
    constructor or an assignment operator then it will probably need all three.
    So for instance if you have a pointer in a class AND you delete that pointer
    in the destructor then you are going to need a copy constructor and
    assignment operator.

    > If don't pass objects of such class as value in a function and don't do
    > assignment, should copy constructor and assignment operator be

    unnecessary?
    >


    No they aren't. But for safety's sake in this case you should declare the
    copy constructor and assignment operator as unimplemented private methods.
    This will prevent them being called accidentally.

    > 2. If a base class have a pointer member, should its derived classes also
    > need copy constructor and assignment operator, no matter if a derived

    class
    > itself has a pointer member or not?


    No. It's the same rule as above. If the derived class (only) has a
    destructor or an assignment operator or a copy constructor then its probably
    going to need all three.

    >
    > 3. If such a pointer member of pointer type of "void", such void type can

    be
    > casted to any other type at runtime, how to code a copy constructor for

    such
    > a member?


    There's no answer to that. It depends.

    john
     
    John Harrison, Sep 14, 2004
    #4
  5. Victor Bazarov <> wrote in message news:<qgD1d.2030$09.us.to.verio.net>...
    > away wrote:
    > > 1. When a class defined with a member of pointer type, it's necessary to
    > > have a copy constructor and assignment operator.

    >
    > Depends on what the pointer points to.


    To clarify, the usual reason for implementing a copy constructor and
    assignment operator is that a class manages or "owns" some resource.
    Such classes often contain pointers, but the presence of a pointer
    is not a reliable guideline. A better guideline is whether a class
    requires a non-trivial destructor.

    Google or search the comp.lang.c++ FAQ for "rule of three"; in short,
    if a class requires a destructor, copy constructor, or assignment
    operator then it probably requires all three of them.

    > > If don't pass objects of such class as value in a function and don't do
    > > assignment, should copy constructor and assignment operator be unnecessary?

    >
    > Depends on existence of other operations that may require copy
    > construction (like storing in a standard container).


    Objects can be copied unintentionally, resulting in subtle bugs;
    for example, due to implicit conversions. You can prevent this by
    declaring a private copy constructor and assignment operator, and
    not defining them.

    > > 2. If a base class have a pointer member, should its derived classes also
    > > need copy constructor and assignment operator, no matter if a derived class
    > > itself has a pointer member or not?

    >
    > Probably not, depends on the derived class.


    Right. If you don't define a copy constructor the compiler will
    generate one that performs a member-by-member copy; similarly for
    assignment.

    Thus if you have a class X with a member of type std::string and
    no user-defined copy ctor, and you copy one X to another, the
    std::string copy ctor will be used to copy the string member.
     
    Niklas Borson, Sep 14, 2004
    #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,902
    Tris Orendorff
    Apr 29, 2004
  2. Aire
    Replies:
    3
    Views:
    470
    Mike Wahler
    Jan 25, 2004
  3. ali
    Replies:
    4
    Views:
    579
    David Harmon
    Mar 5, 2007
  4. Generic Usenet Account
    Replies:
    10
    Views:
    2,248
  5. cinsk
    Replies:
    35
    Views:
    2,616
    James Kanze
    Oct 11, 2010
Loading...

Share This Page