Copy Constructors 0.o?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Copy Constructors 0.o?

Post 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?
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Copy Constructors 0.o?

Post 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.
XianForce
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 767
Joined: Wed Oct 29, 2008 8:36 pm

Re: Copy Constructors 0.o?

Post 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
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Copy Constructors 0.o?

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Copy Constructors 0.o?

Post by Falco Girgis »

I recommend always passing (especially large) objects by reference.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Copy Constructors 0.o?

Post 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...
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply