[LC++]Copy constructor etc

Dan hursh hursh at sparc.isl.net
Wed Jul 25 07:46:10 UTC 2001


1. Well assuming that it's

	C1 = C2;

and not

	myClass C1 = C2;

then it is operator=.  (The default one if you don't declare one.)  The
first example is copying over what is in C1, so operator= has to be
prepared to deallocate something if need be.  A copy constructor is
starting fresh and does not need to (and probably never should) deallocate
member data first.

2. I'd love to tell you it does the vector copy, but I can't remember.
Part of me wants to say it does a bit copy, but another part wants to say
it calls operator= for each member.  That part of me wants to believe that
vector has an operator= that copies the vector and not just the pointer.
Unfortunatly, I don't have a good reference infront of me.  To be honest,
I guess I have never seen a good reference for the STL.  Just HOWTOs and
the like.

Dan

On Tue, 24 Jul 2001, Mark Phillips wrote:

> Suppose I have a class defined as follows:
> 
> class myClass {
>   public:
>     vector<int> myVec;
> }
> 
> and suppose C1 and C2 are to instances
> of myClass and somewhere down the
> track I write the code:
> 
> C1=C2;
> 
> I have a few questions.
> 
> 1. Is the copy constructor called, or is
> operator= called?  (Well obviously
> there is no "operator=" defined for
> this class, but what if there were?)
> 
> 2. Assuming it is the copy constructor
> which is called, myClass will use the
> default copy constructor.  Will the
> default one behave appropriately in this
> case?  Ie does it do a straight bit copy,
> or will it use the vector copy constructor?
> 
> 
> Thanks,
> 
> Mark.
> 
> 
> _______________________________________________
> This is the Linux C++ Programming List
> : http://lists.linux.org.au/listinfo/tuxcpprogramming List
> 




More information about the tuxCPProgramming mailing list