简体   繁体   English

C ++:static一次初始化一个数组成员,成员

[英]C++: static initialize an array member, member at a time

I now I can do this in global scope and everything works fine: 我现在可以在全球范围内做到这一点,一切正常:

const char* Foo::bars[3] = {"a", "b", "c"};

But I want to do this because this is much more clearer and self documenting (especially if you use Enums as the index): 但我想这样做是因为这更加清晰和自我记录(特别是如果你使用Enums作为索引):

const char* Foo::bars[3];
bars[0] = "a";
bars[1] = "b";
bars[2] = "c";

Is it anyway possible? 无论如何可能吗?

I know I can do this inside a function (for example, the class's constructor) but what if the constructor is not called in the start of the program and I want to use the static array? 我知道我可以在函数内部执行此操作(例如,类的构造函数)但是如果在程序的开头没有调用构造函数并且我想使用静态数组呢? That leads to problems. 这导致了问题。

How about this? 这个怎么样?

const char* Foo::bars[3] = {
/* Index    Value */
/* 0 */     "a",
/* 1 */     "b",
/* 2 */     "c"
};

I often use this "technique" to make the initialization of arrays of structs look like a self-documenting spreadsheet. 我经常使用这种“技术”来使结构数组的初始化看起来像一个自我记录的电子表格。

In C++ there is no equivalent of the static Java block. 在C ++中,没有static Java块的等价物。

If you really want to initialize the array automatically, you can create a simple class to do the job: 如果您真的想自动初始化数组,可以创建一个简单的类来完成工作:

// in .cpp
class FooInitializer {
public:
    FooInitializer() {
        Foo:bars[0] = "a";
        Foo:bars[1] = "b";
        Foo:bars[2] = "c";
    }
};

static FooInitializer fooInitializer;

You can use an accessor-function: 您可以使用访问者功能:

const char* GetArray()
{
    static char* result[3];
    static isInitialized = false;
    if (!isInitialized)
    {
        result[0] = "a";
        result[1] = "b";
        result[3] = "c";
        initialized=true;
    }
    return result;
}

Here's another solution that uses the Singleton pattern. 这是使用Singleton模式的另一种解决方案。 Note that the array is initialized once in the singleton's constructor. 请注意,数组在singleton的构造函数中初始化一次。 Also note that this is not thread-safe. 另请注意,这不是线程安全的。 Also beware of the evilness of singletons (search this site for "singleton" to find some interesting debate on this matter). 还要注意单身人士的邪恶(在这个网站上搜索“单身人士”以找到关于此事的一些有趣的辩论)。

#include <iostream>

class StringTable
{
public:
    enum StringId
    {
        hello,
        bye,
        goodDay,
        stringCount
    };

    static const char* lookup(StringId id) {return instance().table_[id];}

private:
    StringTable()
    {
        table_[hello] = "Hello World!\n";
        table_[bye] = "Goobye, cruel world!\n";
        table_[goodDay] = "I said good day!\n";
    }

    static StringTable& instance()
    {
        static StringTable theInstance;
        return theInstance;
    }

    const char* table_[stringCount];
};


int main()
{
    std::cout << StringTable::lookup(StringTable::hello)
              << StringTable::lookup(StringTable::bye)
              << StringTable::lookup(StringTable::goodDay);
}

You probably want an extra const in bars 你可能想要一个额外的const吧

const char* const Foo::bars[3] = 
{
   "a",
   "b",
   "c"
};

The way you declared it, you can actually do what you wanted in setting the members one at a time, although you'd use an "init" function to do it. 你声明它的方式,你实际上可以做你想要的一次一个成员,虽然你使用“init”函数来做到这一点。

If you do want it const, which is probably preferable, it will subsequently become illegal to assign them a line at a time, even in some kind of "init" method and you should simply use a code layout to make it clearer what you are doing. 如果你确实想要const,这可能是最好的,那么一次为它们分配一行就会变得非法,即使在某种“init”方法中,你应该简单地使用代码布局来使它更清楚你是什么这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM