// depending on "type"
memberpointers = new type;
memcpy(memberpointers,s.memberpointers);
// copy constructor
memberpointers = new type(s.memberpointers);
}
}
it is a very basic explanation but it should get you started Repost if you run into problems. What you want to be careful of are shallow copies. That is when you have a pointer and you dont allocate new memory for it but instead just set your pointer to the original pointer value. If the old object goes out of scope, you pointer to its memory block still exists BUT that block of memory has been deleted.
If you have a Matrix class, the designer of the class probably overloaded operator= to do the copy; in that case, it's incorrect to use memcpy to copy the matrix and you must use the assignment operator.
If you just have a simple int[4][4], though, you must use memcpy because the assignment operator won't work. It will compile, but you'll end up having two pointers that refer to the same matrix; changing one will also change the other. You never actually made a copy. This is what Zyrenthian means when he says "shallow copy." This is opposed to a "deep copy," which actually does make a duplicate and gets you two separate matrices (and requires memcpy or something similar).
in other words you must be aware of what you are actually
copying. pointers or whole structures. what you want to accomplish will determine the type of copying you will do.
I wonder if you are refering to creating a new structure
and individually copying the contents.
in this case the memcpy will sve some lines of code. but
the individual items have to be copied separately sooner or later.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.