0

I am trying to access a private static variable (*PhysicsEngine::_world->setDebugDrawer(&debugDraw);*) from another class.

First class:

namespace GameEngine
{
    class PhysicsEngine
    {
    private:
        // Pointer to Bullet's World simulation
        static btDynamicsWorld* _world;

Second class:

bool Game::initialise()
    {
        _device = irr::createDevice(irr::video::EDT_OPENGL,
                                    _dimensions,
                                    16,
                                    false,
                                    false,
                                    false,
                                    &inputHandler);

        if(!_device)
        {
            std::cerr << "Error creating device" << std::endl;
            return false;
        }
        _device->setWindowCaption(_caption.c_str());

    //////////////
    DebugDraw debugDraw(game._device);
    debugDraw.setDebugMode(
    btIDebugDraw::DBG_DrawWireframe |
    btIDebugDraw::DBG_DrawAabb |
    btIDebugDraw::DBG_DrawContactPoints |
    //btIDebugDraw::DBG_DrawText |
    //btIDebugDraw::DBG_DrawConstraintLimits |
    btIDebugDraw::DBG_DrawConstraints //|
    );
    PhysicsEngine::_world->setDebugDrawer(&debugDraw);

If I make _world public I get Unhandled exception at 0x00EC6910 in Bullet01.exe: 0xC0000005: Access violation reading location 0x00000000.

6
  • if you wanna reach it from outside why not make it public then? Commented Apr 10, 2013 at 13:56
  • 6
    Sounds like you need some friends Commented Apr 10, 2013 at 13:56
  • @CaptainObvlious or proper design Commented Apr 10, 2013 at 14:02
  • You don't get exception because of variable access type... Commented Apr 10, 2013 at 14:04
  • Instead write a public method in PhysicsEngine that takes debugDraw and would call the setDebugDrawer on _world and set the debugDraw. Commented Apr 10, 2013 at 14:07

2 Answers 2

1

Expose some static function in the Physics Engine class which returns a reference or pointer to the private static variable _world then call that static function.

PhysicsEngine::getWorld()->setDebugDrawer(&debugDraw);

Expose the below method

static btDynamicsWorld* getWorld() { return _world; }
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you that worked... however I still get the access violation. I probably should look more into Bullet Physics.
What access violation do you get? Did you put the above method under public access?
I realised my stupidity and placed it into Private, I thought worked but I just don't get the access violation. Now I get error C2248: 'GameEngine::PhysicsEngine::getWorld' : cannot access private member declared in class 'GameEngine::PhysicsEngine'
0

Declare that class as Friend in this class. Then the member functions of that class can access this private static member.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.