加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 我可以从另一个函数中初始化静态成员吗???

发布时间:2020-12-16 09:45:46 所属栏目:百科 来源:网络整理
导读:结构中有一个静态成员,因为它在析构函数中是必需的. struct Form{ // ... ~Form() { // access World here } static btDynamicsWorld *World;}; 有没有办法从另一个函数中初始化这个静态成员? void ModulePhysics::Init(){ // ... btDynamicsWorld *Form::W
结构中有一个静态成员,因为它在析构函数中是必需的.

struct Form
{
    // ...
    ~Form()
    {
        // access World here
    }
    static btDynamicsWorld *World;
};

有没有办法从另一个函数中初始化这个静态成员?

void ModulePhysics::Init()
{
    // ...
    btDynamicsWorld *Form::World = /* ... */;
}

我当前的代码导致这两个编译器错误.

Error 1 error C2655: ‘Form::World’ : definition or redeclaration illegal in current scope

Error 2 error C2086: ‘btDynamicsWorld *Form::World’ : redefinition

解决方法

不,你不能.但是您可以将其初始化为NULL,并且在函数中,如果它为NULL,则执行实际初始化.

编辑:提供一个示例:

void ModulePhysics::Init()
{
    // ...
    if(Form::World == NULL)
    {
        // The real initialization
    }
}

某处,在文件范围内(在C文件中,而不是在标题中!):

btDynamicsWorld* Form::World = NULL;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读