Copying Pointers does not do the job.

This is an old ar­ti­cle from 2012. I was a kid. I learned a lot since then, so please do think twice when tak­ing ad­vice from me as a kid.

When shoot­ing bul­lets, en­ti­ties must be able to copy a bul­let en­ti­ty. That bul­let en­ti­ty is stored as a point­er in that en­ti­ty, which has the abil­i­ty to shoot. Okay, then let’s cre­ate a copy-con­struc­tor and cre­ate a new in­stance as a copy of that.

So what’s the prob­lem? The en­ti­ty has a point­er point­ing at a graph­ics node. Okay, then let’s cre­ate a copy-con­struc­tor for that as well. But we don’t know the spe­cif­ic type of this node (Node has a lot of chil­dren: SpriteN­ode, TextN­ode etc.), and there­fore don’t know which copy-con­struc­tor to call.

I found a so­lu­tion:

Node* Node::copy() { return new Node(*this); }

I got this as a vir­tu­al method for ev­ery class de­rived from Node, so they call their own copy-con­struc­tors and give back a point­er to the re­sult.

This post was im­port­ed from tum­blr


Archived blog posts from squareys.tum­