what does default operator= do?

Discussion in 'C++' started by cppaddict, May 26, 2004.

  1. cppaddict

    cppaddict Guest

    Hi,

    How, in general, does the default operator= work?

    That is, say I have a custom class MyClass and do:

    MyClass defaultMyClass;
    MyClass initializedMyClass("set","some","members");
    defaultMyClass = initializedMyClass;

    Will that final line copy all of the members if initialized class into
    defaultMyClass? A deep copy? If not, how does it work?

    Thanks,
    cpp
     
    cppaddict, May 26, 2004
    #1
    1. Advertising

  2. cppaddict wrote:
    >
    > Hi,
    >
    > How, in general, does the default operator= work?
    >
    > That is, say I have a custom class MyClass and do:
    >
    > MyClass defaultMyClass;
    > MyClass initializedMyClass("set","some","members");
    > defaultMyClass = initializedMyClass;
    >
    > Will that final line copy all of the members if initialized class into
    > defaultMyClass? A deep copy? If not, how does it work?
    >


    The default generated operator= does the only sensible thing
    it can do: do a memberwise assignment.

    It depends on the internals of your class if this is a deep copy
    or not.


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, May 26, 2004
    #2
    1. Advertising

  3. cppaddict wrote:
    > How, in general, does the default operator= work?


    In invokes operator= semantics for every base class and every member,
    in the declaration order.

    > That is, say I have a custom class MyClass and do:
    >
    > MyClass defaultMyClass;
    > MyClass initializedMyClass("set","some","members");
    > defaultMyClass = initializedMyClass;
    >
    > Will that final line copy all of the members if initialized class into
    > defaultMyClass? A deep copy? If not, how does it work?


    It's called "member-wise assignment". It all depends on the types used
    and whether the assignment operators are defined to do anything different
    than the default behaviour.

    Also, since it has some bearing on it, read about "The Rule of Three".

    Victor
     
    Victor Bazarov, May 26, 2004
    #3
  4. cppaddict

    cppaddict Guest

    >It depends on the internals of your class if this is a deep copy
    >or not.


    Thanks for your reply.

    What does ithe deepness of the copy depend on? Eg, if one of the
    members of my class is a vector which contains other custom objects,
    will a deep copy of the vector be made?

    Thanks,
    cpp
     
    cppaddict, May 26, 2004
    #4
  5. "cppaddict" <> skrev i en meddelelse
    news:...
    > >It depends on the internals of your class if this is a deep copy
    > >or not.

    >
    > Thanks for your reply.
    >
    > What does ithe deepness of the copy depend on? Eg, if one of the
    > members of my class is a vector which contains other custom objects,
    > will a deep copy of the vector be made?
    >
    > Thanks,
    > cpp

    Yes. Any standard collection will be copied correctly. Pointers are copied
    too, but their content is not - and this might be problematic wrg e.g.
    ownership issues.

    /Peter
     
    Peter Koch Larsen, May 26, 2004
    #5
  6. cppaddict wrote:
    >
    > >It depends on the internals of your class if this is a deep copy
    > >or not.

    >
    > Thanks for your reply.
    >
    > What does ithe deepness of the copy depend on? Eg, if one of the
    > members of my class is a vector which contains other custom objects,
    > will a deep copy of the vector be made?


    Yes.

    As a simple rule of thumb.
    If you don't need to do any special action in the destructor
    to handle a specific member variable, then the copy constructor
    and the assignment operator generated by the compiler will do
    the right thing.

    To your question: Assume there is a std::vector in your
    class. You don't need to do anything in the destructor
    to free that vector, because the vector handles
    everything by itself. But so does it handle everythign
    for itself when it is assigned to another vector.
    When the compiler generates the code for op=, it uses
    memberwise assignment to do this. This means that it
    will tell the vector to assign itself to another vector
    and since a vector knows how to do this, everything
    is correct.

    PS)
    The above is called: rule of three
    Whenever you need one of
    * destructor
    * copy constructor
    * assignment operator
    you most likely need all 3 of them

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, May 27, 2004
    #6
    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. micky

    default.mspx or default.do

    micky, Dec 16, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    496
    Andrew Robinson
    Dec 17, 2005
  2. Gaetan
    Replies:
    3
    Views:
    1,433
    Juan T. Llibre
    Feb 14, 2006
  3. John Smith
    Replies:
    2
    Views:
    425
    Ivan Vecerina
    Oct 6, 2004
  4. Tarun
    Replies:
    5
    Views:
    395
    Tarun
    Jul 14, 2005
  5. puzzlecracker
    Replies:
    8
    Views:
    434
    James Kanze
    Apr 15, 2008
Loading...

Share This Page