为什么当c ++构造函数作为静态成员变量出现时未被调用? [英] why the c++ constructor was not called when it appear as the static member variable?

查看:139
本文介绍了为什么当c ++构造函数作为静态成员变量出现时未被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个奇怪的问题,

I had a strange problem ,

在A class中声明一个静态成员变量,其名称为B class。并在cpp文件中初始化。但是从未调用过B类的构造函数。我尝试使用一些小型测试,可以正常调用测试构造函数。

declare a static member variable whose name is B class in A class . And initialize in the cpp file. but the constructor of B class was never called. I try to use some small test , the test constructor could be called normally. so it is very strange for our production system.

像这样的代码,在 hpp 中:

class Test
{
    public:
    Test()
    {
        ofstream file("/tmp/wup.txt",ios::app);
        file << "wup in test" << endl;
        file.close();
    }
};

//## An extended personality
class TsdNAExtPersonality : public TsdNAPersonality{
public:

  TsdNAExtPersonality(
        s_gg62_personRec * gg62Header,
                   TsdNAFunctionType requiredFunctionType);
private:
  static Test test;

public:
  TsdNAExtPersonality( string * personalityFile, TsdNAFunctionType requiredFunctionType);
};

然后在另一个 cpp 文件中,我用

And in another cpp file I initialize with

Test TsdNAExtPersonality::test;

我尝试了几种方法,但是我发现所有方法都没有用处。

I have tried in several way, but i found all of ways are unusefull.


  1. 没有将变量设置为成员变量,而是全局变量==>也无法输出

  2. 更改成员变量作为指针,并使用new ==> no

更改初始化方式,环境为HP-UX,编译为aCC

the environment is HP-UX ,and the compile is aCC

所以我的问题是:


  1. 是否有任何编译选项会影响变量吗?换句话说,所有静态变量都不会被初始化。

  1. is there any compile option will influence the variable ? in other words, all the static variable will not be initialized.

从C ++标准开始,应该在加载库时调用它,对吧?

from the standard of C++ it should be called when the library was load, right?

我使用相同的方式放置了另一个静态int值,可以对其进行初始化。但是没有调用类构造函数,这很奇怪。

I put another static int value using the same way, it could be initialized. but the class constructor is not called , very strange.

我的代码中是否有任何错误?

is there any mistake in my code ?


推荐答案


从C ++标准开始,应该在加载库时调用它,对吧?

from the standard of C++ it should be called when the library was load, right?

否。具有静态存储持续时间的对象的动态初始化可以保证在执行同一转换单元中定义的任何功能之前进行。如果没有这样的函数,或者您的程序从未调用过它们,那么就无法保证它将被初始化。

No. Dynamic initialisation of an object with static storage duration is guaranteed to happen before execution of any function defined in the same translation unit. If there are no such functions, or your program never calls them, then there's no guarantee that it will ever be initialised.


静态int值可以使用相同的方式进行初始化。但是没有调用类构造函数,这很奇怪。

I put another static int value using the same way, it could be initialized. but the class constructor is not called , very strange.

一个 int 变量


是否有任何编译选项会影响变量?

is there any compile option will influence the variable ?

我不知道,但是我不熟悉您的平台。您可以通过在函数内对其进行范围界定来赋予自己更多控制权:

Not that I know of, but I'm not familiar with your platform. You could give yourself more control over the object's creation by scoping it within a function:

static Test & test() {
    static Test test;
    return test;
}

现在可以保证在第一次调用该函数时将其初始化。当然,您需要记住在某个时候调用它。

Now it is guaranteed to be initialised the first time the function is called. Of course, you'll need to remember to call it at some point.

这篇关于为什么当c ++构造函数作为静态成员变量出现时未被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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