Page 1 of 1

Copy Constructors 0.o?

Posted: Wed Sep 30, 2009 11:36 pm
by XianForce
Well I've been reading this book, Programming Game AI by Example (great book by the way, I'd highly reccommend it =D). Anyways I've noticed the use of copy constructors, and I've seen them from Sam's Teach Yourself C++ in an Hour a Day, but it barely touches on it. I looked around through Google, but I still don't understand the purpose of copy constructors.

So can anyone give an explanation on them?

Re: Copy Constructors 0.o?

Posted: Thu Oct 01, 2009 4:11 am
by K-Bal
The copy constructor is always implemented, even if you don't write it down in your class. However, if it does not do what you want, you are free to make your own implementation of it.

Re: Copy Constructors 0.o?

Posted: Thu Oct 01, 2009 8:24 am
by XianForce
K-Bal wrote:The copy constructor is always implemented, even if you don't write it down in your class. However, if it does not do what you want, you are free to make your own implementation of it.
Yeah, I understand that bit, but WHY are they implement, what is the purpose of them being there?

Found an Article that explains everything =D

Re: Copy Constructors 0.o?

Posted: Thu Oct 01, 2009 1:32 pm
by MarauderIIC
In response to your strikethrough =) They're for copying things. You don't have to implement your own unless you need a deep copy (as opposed to a shallow copy), or other special treatment.

Re: Copy Constructors 0.o?

Posted: Thu Oct 01, 2009 2:26 pm
by Falco Girgis
I recommend always passing (especially large) objects by reference.

Re: Copy Constructors 0.o?

Posted: Sun Oct 04, 2009 6:46 pm
by MarauderIIC
GyroVorbis wrote:I recommend always passing (especially large) objects by reference.
Right, but if you want a copy of something to modify, say you want to clone 20 enemies as a special power or something...