Thread: question about assignment operators/copy constructors (c++)

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    25

    question about assignment operators/copy constructors (c++)

    i am trying to make a new class object (MySquareMatrix)by doing this:

    MySquareMatrix C(A); // making a copy of 'A' called 'C'

    the only thing is that when i try to access 'A' later, it has the values of C in it. i am using a copy constructor and i am assuming that 'C' is stored in the location where 'A' was stored. how do i create 'C' in a new location? (but still being a copy of 'A')

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I'm assuming that your MySquareMatrix contains pointers or references.

    In your copy constructor you'll have to copy across the data pointed to and not just the pointers otherwise both instances will point to the same area in memory. Which will probably mean allocating memory and copying the contents across. Post some code if you need something more specific.
    zen

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    Based on the last reply i'm also assuming that you are using pointers or references...in this case what you are doing is performing a "member-wise copy" -- a so called "shallow copy"... what you need to do is perform a "deep copy" by simply declaring your own copy constructor, obviously your implementation of the copy constructor would depend on the memory allocation, meaning whether let's say you allocated your memory on the stack (i assume you did) or allocated it on the free store(heap) by using the "new" operator....
    if you are using pointers (i assume) you don't really want to have a default "compiler generated" copy constructor, b/c all it does is copy the member variables (as in their addresses) what you would have then so called "alliasing", which is (for instance) 2 pointer objects pointing to the same memory cell...and after one gets destroyed then the other one is still pointing to the same spot, but now nothing would be there, what you would create by this is a "stray pointer" and if would call on that remaining pointer the result could be catastrophic....(of course there a lot more to it,actually a ton of more stuff...so let's leave it at that)
    but what would really help if we would see some code....then i am sure that you would get a sufficient answer..

    Regards,
    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Assignment operator overloading
    By hsgw in forum C++ Programming
    Replies: 1
    Last Post: 01-20-2007, 06:44 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM