Thread: Base class initialization

  1. #1
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607

    Base class initialization

    Ok I've got a huge class hierarchy here and I'm getting myself confused over what's going on.

    Here is the structure.

    Code:
    class D3D {};
    class DirectDraw {};
    class DXGraphics:public D3D, public DirectDraw {};
    
    class DXInput {};
    class DXMouse:public DXInput {};
    
    class DXEngine:public DXGraphics,public DXInput {};
    
    class TileEngine:public DXEngine {};
    All constructors contain no code -> just default constructors.
    The Init() function of each class actually inits the class -> left out for clarity's sake.

    Here is my problem. Let's say I wanted to use the mouse. The mouse requires the DirectInput object to be created first. Then you use that object to then create the Mouse object. The Mouse object requires you to pass the HINSTANCE of the application so that it can set the priority level for this instance.

    Problem with this setup is that I cannot create a mouse at all. What is happening when I instantiate TileEngine? Does it not instantiate DXMouse? Since I know it does, how then do I actually create the Mouse object. Every time I try to place a pointer to the mouse object inside of TileEngine and instantiate it, I get an illegal operation message box - or the code simply exits with no error. Since DXMouse is actually a part of TileEngine, why should I even include a class pointer to DXMouse? How do I actually create the DXMouse object?

    Here is what happens when I instantiate TileEngine.

    D3D is instantiated.
    DirectDraw is instantiated.
    DXGraphics is instantiated.
    DXInput is instantiated.
    DXMouse is instantiated.
    DXEngine is instantiated.

    It's easy to use DirectDraw and D3D because these are mere function calls. Hmm. Maybe I just answered my own question. Since I can directly use DirectX and D3D from TileEngine could I also not just directly use the mouse with no pointer? In other words simply call its functions to use it?

    All this inheritance crapola is for the birds. Perhaps I will use pointers to these objects instead of deriving from everything and using multiple base classes.
    Last edited by VirtualAce; 01-10-2004 at 09:33 PM.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Draw out your class heirachy. You will notice that DXEngine derives from DXGraphics and DXInput. So how would it know about DXMouse which independently derives from DXInput also. Mouse isn't getting instantiated. I'm not sure though why putting a pointer to DXMouse and creating it wouldn't work, it should work fine.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    class TileEngine:public DXEngine
    {
       DXMouse *theMouse;
       public:
         TileEngine(HWINDOW MainWindow);
    };
    
    TileEngine::TileEngine(HWND MainWindow)
    {
      theMouse=new DXMouse(MainWindow);
    }
    
    TileEngine::GameInit(void)
    {
      theMouse->some function() ...........fails here as if null pointer
    }
    Sorry for the confusion. The Mouse requires HWND not HINSTANCE. So DirectInput is being created right? So LPDIRECTINPUT lpdi (omitted here) is valid if its in the DXInput constructor?

    Basically what you are saying is that only the base classes are instantiated?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Bubba
    Code:
    class TileEngine:public DXEngine
    {
       DXMouse *theMouse;
       public:
         TileEngine(HWINDOW MainWindow);
    };
    
    TileEngine::TileEngine(HWND MainWindow)
    {
      theMouse=new DXMouse(MainWindow);
    }
    
    TileEngine::GameInit(void)
    {
      theMouse->some function() ...........fails here as if null pointer
    }
    Sorry for the confusion. The Mouse requires HWND not HINSTANCE. So DirectInput is being created right? So LPDIRECTINPUT lpdi (omitted here) is valid if its in the DXInput constructor?

    Basically what you are saying is that only the base classes are instantiated?
    I really don't see any problems with what you did. Did you step through and make sure everything is fine?

    Whenever you create your TileEngine class it basically creates the classes you derived TileEngine from and works its way up. So when it creates DXGraphics it'll create D3D and DDraw but when you create DXInput that is the base class, so DXMouse is never created.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You seem to create two DXInput objects (once as base of TileEngine via DXEngine and once as base of DXMouse). Maybe you should rethink your hierarchy. Make DXMouse NOT derive from DXInput, instead let DXInput be a creation object for DXMouse, DXKeyboard and DXJoystick.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Virtual base class
    By George2 in forum C++ Programming
    Replies: 7
    Last Post: 03-14-2008, 07:45 AM
  2. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. Can't Access the private member from base class
    By planet_abhi in forum C# Programming
    Replies: 3
    Last Post: 01-09-2006, 04:30 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM