This is a discussion on Problems with derived classes, copy constructors, *lots of example source included* within the C++ Programming forums, part of the General Programming Boards category; DISCLAIMER: If you are going to read this code to learn from it, I should warn you you might pick ...

DISCLAIMER: If you are going to read this code to learn from it, I should warn you you might pick up some bad habits. Furthermore, the "game" this source comprises is a sandbox. It has few rules, no point, and is quite silly-looking. I'm only using it to develop the engine, at which point I plan to throw the "game" away and build a new game on this engine.

* You will need the DirectX 6 SDK to compile the program. The DirectX 8 SDK doesn't seem to work for me due to Microsoft's design changes. The project files included are for Microsoft Visual C++ 6 *

The attached code is an adaptation of Andre Lamothe's Game Programming for Dummies engine. It may help to have read this book to understand the source, but should not be a requirement. I have re-written it in C++ so I can take advantage of polymorphism to easily create new object types. I didn't post this to the Game Programming section because my problem is more with C++ concepts of polymorphism, derived classes, copy constructors, etc. I've done a lot of research on this problem and I can't seem to track it down! The problem is that only one of the BOBTYPE_POPUP objects created in Game_Init() appears on the screen. The objects appear to have been created properly, and if you read the .LOG file everything appears to be hunky-dory, but only the first popup appears on the screen and the others cannot be seen or collided with. Also, there are minor bugs with colliding objects (i.e. you and the Chef) sticking together, or disappearing off the edge of the screen. Any help on these two points, or other suggestions to improve the organization or design of the source would be greatly appreciated. For those of you wishing to learn from this source, feel free to ask me any technical questions, and I will answer them as best I can. If you improve on this design, please post your improvements so we can all benefit from it.

More information

Some more information on the problem:

It's not that only one of the bobPopUp objects are appearing, it's that all 5 are appearing in the same position. Take a look at the log excerpt below (only the last 2 bobPopUp clonings are shown, along with some debug info I programmed in after the fact to test the positions of the bobPopUps after cloning them all, and dump the pointer values for each of the bobPopUp objects to the log):

You will notice that all the bobPopUps successfully received random screen positions. Then at the end their positions and the position of the template are all equal in value to the position of the last clone. This means modifying the x or y value of any of the bobPopUp objects modifies the x or y value of all of them, including the template (the line "x 213 y 525 of template" shows the position of the template object after cloning completed). Is this a copy constructor problem or something? Someone *please* help, I'm going nuts!

I don't have time to look into it properly, but I think the line that may be causing the problems is

bob[iCurrentBOB]=templates[BOBTYPE_POPUP][0];

(The line may be slightly different in your original, I was playing around and can't remember if it was exactly like this).

in your Game_Init() function in SnowWars.cpp. It appears that bob and templates are arrays of pointers (not arrays of objects). So by assigning one pointer to another you are just assigning addresses (not objects) and so your operator=() isn't ever called. You will probably have to de-reference at least the destination pointer before assigning.