![](/img/trans.png)
[英]How to access private static variable in static member function of another class?
[英]C++ How to access private static variable in another class
我正在尝试从另一个类访问私有静态变量(* PhysicsEngine :: _world- > setDebugDrawer(&debugDraw); *)。
头等舱:
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设为公开,则Bullet01.exe中的0x00EC6910会出现未处理的异常:0xC0000005:访问冲突读取位置0x00000000。
在Physics Engine类中公开一些静态函数,该类将返回对私有静态变量_world的引用或指针,然后调用该静态函数。
PhysicsEngine::getWorld()->setDebugDrawer(&debugDraw);
公开以下方法
static btDynamicsWorld* getWorld() { return _world; }
在该课程中将该课程宣布为“朋友”。 然后,该类的成员函数可以访问此私有静态成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.