静态变量未初始化 [英] Static variable not initialized

查看:249
本文介绍了静态变量未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,静态变量,显然没有初始化,因为它应该是。

我有一个巨大的项目,运行在Windows和Linux。由于Linux开发人员没有这个问题,我建议这是一种有线的Visual Studio的东西。


头文件

  class MyClass 
{
// some其他的东西在这里
...
private:
static AnotherClass * const Default_;
};




CPP档案

  AnotherClass * const MyClass :: Default_(new AnotherClass()); 
MyClass(AnotherClass * const var)
{
assert(Default_);
...
}

问题是默认_ 始终为 NULL 。我也试过一个断点在该变量的初始化,但我不能抓住它。


在另一个类中有一个类似的问题。

CPP档案

  std :: string const MyClass :: MyString_(someText); 
MyClass :: MyClass()
{
assert(MyString_!=);
...
}

在这种情况下 MyString _ 始终为空。所以再次没有初始化。

有没有人有这个想法?这是一个Visual Studio设置问题吗?

Cheers Simon



编辑:

我也遇到了静态初始化失败。但我不知道如果这可能是问题,因为没有与Linux编译器的问题。在这种情况下,编译器是否应该以同样的方式进行反应?

解决方案

我建议你使用静态变量,静态变量本身:

  class MyClass 
{
//这里的其他东西
。 ..
private:
static AnotherClass * const getAnotherClass();
};

AnotherClass * const MyClass :: getAnotherClass()
{
static AnotherClass * const p = new AnotherClass();
return(p);
}

标准保证在初始化函数时p被初始化一次时间,所以你将永远得到正确初始化的对象(除非你已经耗尽内存或你的构造函数抛出)。



请注意 - 这可能或可能不是线程安全(取决于你的编译器真的)。



还有一个注意事项 - 现在你必须忍受内存泄漏,因为它是真的不可能决定什么时候销毁对象,你有没有办法将p重置为NULL。


I've got a strange problem with a static variable that is obviously not initialized as it should be.
I have a huge project that runs with Windows and Linux. As the Linux developer doesn't have this problem I would suggest that this is some kind of wired Visual Studio stuff.

Header file

class MyClass
{
    // some other stuff here
    ...
    private:
        static AnotherClass* const Default_;
};


CPP file

AnotherClass* const MyClass::Default_(new AnotherClass(""));
MyClass(AnotherClass* const var)
{
    assert(Default_);
    ...
}

Problem is that Default_is always NULL. I also tried a breakpoint at the initialization of that variable but I cannot catch it.

There is a similar problem in another class.
CPP file

std::string const MyClass::MyString_ ("someText");
MyClass::MyClass()
{
    assert(MyString_ != "");
    ...
}

In this case MyString_is always empty. So again not initialized.
Does anyone have an idea about that? Is this a Visual Studio settings problem?
Cheers Simon

Edit:
I also came across the static initialization fiasco. But I'm not sure if that could be the problem because there are no problems with the Linux compiler. Shouldn't the compiler react the same way in this case?

解决方案

I suggest you use static member function with static variable and not static variable itself:

class MyClass
{
    // some other stuff here
    ...
    private:
        static AnotherClass* const getAnotherClass();
};

AnotherClass *const MyClass::getAnotherClass()
{
    static AnotherClass *const p = new AnotherClass("");
    return(p);
}

The standard guarantees that p is initialized once when the function is called for the first time, so you will always get properly initialized object (unless you've already exhausted memory or you constructor threw).

Please note - this may or may not be thread safe (depends on your compiler really).

And yet another note - now you have to live with "memory leak" as it is really next to impossible to decide when to destroy the object and you have NO WAY to reset p to NULL.

这篇关于静态变量未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆