> Ah, I didn't know there were different types of copy constructors. I did
> a search on the web to find out some more information on shallow and deep
> copy constructors. If what I read was correct, the compiler automatically
> implements a shallow copy constructor for your classes?
Yes, by default the compiler implements a shallow copy constuctor (if you don't
specify a copy constructor).
> Also, since the copy constructor is called for a statement like this:
>
> CObject ob1;
> CObject ob2 = ob1;
>
> How would you deal with pointers? I'm not totally sure.
The compiler does a shallow copy (i.e. the pointer values of member variables
are copied, but the contents that the point to are not copied). Both classes
will contain pointers that point to the same address in memory. When one of
them frees this memory, the other one causes access violation crashes trying to
access memory that no longer exists. This is the case where YOU need to
implement a deep copy constructor. For each pointer, you will need to allocate
new memory to store the data being pointed to and memcpy that data from the
source object to the desintation object.
> CObject *pob1 = new CObject( );
> CObject *pob2 = new CObject( pob1 );
>
> Yes? No?
Yes, statement #2 above will use the default shallow copy constructor created
for you by the C++ compiler (isn't that nice of it?) :)
If this is not what you want, you will need to implement your own...
class CObject {
public:
CObject::CObject(const &CObject); // my copy constructor
};
...then implement whatever code is needed in CObject::CObject() constructor to
copy the member variables from obj1 to obj2 with deep copy of pointer data.
Jeffrey "botman" Broome
_______________________________________________
To unsubscribe, edit your list preferences, or view the list archives, please visit:
http://list.valvesoftware.com/mailman/listinfo/hlcoders