Thread: Copy constructors and operator=()

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User filler_bunny's Avatar
    Join Date
    Feb 2003
    Posts
    87

    Copy constructors and operator=()

    I have been reading many texts on these two components of an object, but I am not 100% convinced I understand them.

    But this is what I think I have:

    1) A copy constructor is provided by default, but should be overided in the case you require "deep" copying of your objects data members (i.e. you have allocated memory to pointers and you want the values copied not the pointer).
    2) If you provide your own copy constructor you should also provide your own operator= method.

    I presume that the reason for 2 is that if you have to "deep" copy with the copy constructor, then the same holds true for any assignment...

    [EDIT]
    I think I have this around the wrong way.. If I define a operator=(), THEN I must have a copy constructor..
    [/EDIT]

    But I am not sure I fully understand the purpose of 2. Can anyone enlighten me or correct me?
    Last edited by filler_bunny; 08-24-2003 at 03:40 AM.
    Visual C++ .net
    Windows XP profesional

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A pointer question.
    By joenching in forum C++ Programming
    Replies: 7
    Last Post: 03-20-2008, 04:10 PM
  2. LinkedList operator= and deep copy
    By sethjackson in forum C++ Programming
    Replies: 11
    Last Post: 02-28-2008, 12:54 PM
  3. illegal copy constructor required?
    By ichijoji in forum C++ Programming
    Replies: 1
    Last Post: 03-08-2005, 06:27 PM
  4. Operator=
    By crzy1 in forum C++ Programming
    Replies: 4
    Last Post: 04-02-2003, 10:08 PM
  5. copy constructor
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2002, 02:23 PM