2013-04-10 88 views
0

我想从另一个类访问一个私有静态变量(* PhysicsEngine :: _world - > setDebugDrawer(& debugDraw); *)。C++如何访问另一个类中的私有静态变量

第一类:

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

二等:

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

如果我让_world公共我得到未处理的异常在0x00EC6910在Bullet01.exe:0000005:访问冲突读取位置00000000。

+0

如果你想从外面为什么不把它公开,然后实现它? – 2013-04-10 13:56:22

+6

听起来像你需要一些'朋友' – 2013-04-10 13:56:29

+0

@CaptainObviously或适当的设计 – 2013-04-10 14:02:06

回答

1

在Physics Engine类中公开一些静态函数,它返回一个引用或指向私有静态变量_world的指针,然后调用该静态函数。

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

揭露以下方法

static btDynamicsWorld* getWorld() { return _world; } 
+0

谢谢你的工作......但是我仍然得到访问冲突。我可能应该更多地考虑子弹物理。 – 2013-04-10 14:08:08

+0

你会得到什么访问冲突?您是否已将上述方法置于公开访问之下? – Rush 2013-04-10 14:10:27

+0

我意识到自己的愚蠢,并将其置于私人,我认为工作,但我没有得到访问冲突。现在我得到错误C2248:'GameEngine :: PhysicsEngine :: getWorld':无法访问类'GameEngine :: PhysicsEngine'中声明的私有成员 – 2013-04-10 14:13:58

0

将该班级声明为此班级中的朋友。然后该类的成员函数可以访问这个私有静态成员。

相关问题