gpt4 book ai didi

C++ 如何访问另一个类中的私有(private)静态变量

转载 作者:行者123 更新时间:2023-11-28 07:40:27 28 4
gpt4 key购买 nike

我正在尝试从另一个类访问私有(private)静态变量 (*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 类中公开一些静态函数,该函数返回对私有(private)静态变量 _world 的引用或指针,然后调用该静态函数。

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

公开下面的方法

static btDynamicsWorld* getWorld() { return _world; }

关于C++ 如何访问另一个类中的私有(private)静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15927879/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com