c++如何访问另一个类中的私有静态变量
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公开,我得到未处理的异常在0x00EC6910在Bullet01.exe: 0xC0000005:访问违规读取位置0x00000000。
在物理引擎类中公开一些静态函数,该函数返回私有静态变量_world的引用或指针,然后调用该静态函数。
PhysicsEngine::getWorld()->setDebugDrawer(&debugDraw);
显示下面的方法
static btDynamicsWorld* getWorld() { return _world; }
在这个类中声明这个类为Friend。然后该类的成员函数可以访问这个私有静态成员。
相关文章:
- 在 .h 文件中的类中声明静态变量和在.cpp文件中声明"global"变量有什么区别
- 模板基类中的静态变量
- 类和静态变量
- 不同作用域中的静态变量和全局变量
- 静态变量声明和定义
- 是否可以依赖函数范围的静态变量来执行程序关闭期间调用的方法?
- 在类中继承静态变量?
- "local scope"中的 C++ 初始化静态变量
- 使用静态变量的递归调用的不同输出
- 静态 constexpr 类成员变量对多线程读取是否安全?
- C++:是否可以使用非静态成员变量模板?
- 复制文件流C++静态变量
- 如何在复杂继承中访问静态成员变量
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 跨模板化函数编译的静态变量
- constexpr函数中的静态constexpr变量
- 静态成员变量不会由 gettext 转换
- 从 exe 和 dll 访问静态库中的 extern 变量
- 如果包含映射的静态库与可执行文件和动态库链接,静态映射(变量)是否会被多次释放?
- 用于定义公共变量静态常量整数的C++标准