Thread: Problems with derived classes, copy constructors, *lots of example source included*

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    17

    Problems with derived classes, copy constructors, *lots of example source included*

    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.
    -sh0x

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    17

    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):

    ______________________________________
    Creating popup clone...
    Found free BOB 5
    Construct new BOB
    Increasing instance[4] to 6
    New BOB created
    Template Copied
    x 808 y 631
    w 32 h 32
    ptr 4480476
    x 808 y 631 of template
    Done creating Clone
    Creating popup clone...
    Found free BOB 6
    Construct new BOB
    Increasing instance[4] to 7
    New BOB created
    Template Copied
    x 213 y 525
    w 32 h 32
    ptr 4480480
    x 213 y 525 of template
    Done creating Clone
    Construct new BOB
    Increasing instance[2] to 1
    Construct new BOB
    Increasing instance[1] to 3
    Decreasing instance[1] to 2
    PopUp Positions:
    BOBTYPE_POPUP # 2 213 x 525
    BOBTYPE_POPUP # 3 213 x 525
    BOBTYPE_POPUP # 4 213 x 525
    BOBTYPE_POPUP # 5 213 x 525
    BOBTYPE_POPUP # 6 213 x 525
    ______________________________________

    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!
    -sh0x

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with copy constructors
    By nessie in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2005, 07:50 AM
  2. 'Passing by Refrence for Efficiency', Copy Constructors?
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 07:11 AM
  3. Copy constructors and private constructors
    By Eibro in forum C++ Programming
    Replies: 5
    Last Post: 11-24-2002, 10:16 AM
  4. List of derived classes
    By Asmodan in forum C++ Programming
    Replies: 1
    Last Post: 05-28-2002, 08:46 PM
  5. Inheiritance and derived classes
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-09-2001, 03:50 PM