Andrew Uknown
Andrew Uknown

Reputation: 99

C++ How to access private static variable in another class

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.

Upvotes: 0

Views: 2776

Answers (2)

Rush
Rush

Reputation: 496

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; }

Upvotes: 1

paper.plane
paper.plane

Reputation: 1197

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

Upvotes: 0

Related Questions