在类内初始化固定大小的常量数组

发布于 2024-12-15 23:29:16 字数 211 浏览 3 评论 0原文

考虑以下类:

class A {
    const int arr[2];
public:
      A() { }
};

是否可以从构造函数初始值设定项列表或以除声明它的行以外的任何其他方式初始化 arr(即 const int arr[2] = {1 ,2};)?

请注意,我对与 C++98 一起使用的方法感兴趣!

Consider the following class:

class A {
    const int arr[2];
public:
      A() { }
};

Is it possible to initialize arr from the constructor initializer list or in any other way than on the line where it is declared (i.e. const int arr[2] = {1,2};)?

Note that I'm interested in methods that work with C++98!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无可置疑 2024-12-22 23:29:16

通过将它们包装在 struct 中,例如:

class A
{
    struct Data
    {
        int arr[2];
    };

    Data const arr;
public:
    A() : arr( someOtherStruct ) {}
};

这确实意味着要访问数据,您必须编写 arr.arr
可以通过继承 struct 来避免这种情况:

struct PrivateDataForA
{
    int arr[2];
};

class A : private PrivateDataForA
{
public:
    A() : PrivateDataForA( someOtherStruct ) {}
};

这确实使 struct 的名称在类外部可见
(这可能是一个优势——客户端代码可以将一个作为
争论)。

如果您手头没有该结构的实例,请说因为您想要
要使用根据构造函数的参数计算出的值来填充它,您可以
可以使用静态成员函数:

class A : private PrivateDataForA
{
    static PrivateDataForA createInitializer( int a, int b );
public:
    A( int a, int b ) : PrivateDataForA( createInitializer( a, b ) )
    {
    }
};

对于OP的具体示例:

#include <iostream>
#include <stddef.h>

typedef ptrdiff_t   Size;
typedef Size        Index;

template< class Element, Size n >
struct Array{ Element elem[n]; };

class A {
    Array<int, 2> const arr_;       // const int arr[2];

    A& operator=( A const& );       // No such.

    static Array<int, 2> const& oneAndTwo()
    {
        static Array<int, 2> const a = {1, 2};
        return a;
    }

public:
    A(): arr_( oneAndTwo() ) {}
    int at( Index i ) const { return arr_.elem[i]; }
};


int main()
{
    using namespace std;

    A o;
    for( int i = 0;  i < 2;  ++i )
    {
        cout << o.at( i ) << endl;
    }
}

By wrapping them in a struct, e.g.:

class A
{
    struct Data
    {
        int arr[2];
    };

    Data const arr;
public:
    A() : arr( someOtherStruct ) {}
};

This does mean that to access the data, you'd have to write arr.arr.
It's possible to avoid that by inheriting from the struct:

struct PrivateDataForA
{
    int arr[2];
};

class A : private PrivateDataForA
{
public:
    A() : PrivateDataForA( someOtherStruct ) {}
};

This does make the name of the struct visible outside of the class
(which might be an advantage—client code could pass you one as an
argument).

If you don't have an instance of the struct handy, say because you want
to fill it with values calculated from arguments to the constructor, you
can use a static member function:

class A : private PrivateDataForA
{
    static PrivateDataForA createInitializer( int a, int b );
public:
    A( int a, int b ) : PrivateDataForA( createInitializer( a, b ) )
    {
    }
};

For the OP’s concrete example:

#include <iostream>
#include <stddef.h>

typedef ptrdiff_t   Size;
typedef Size        Index;

template< class Element, Size n >
struct Array{ Element elem[n]; };

class A {
    Array<int, 2> const arr_;       // const int arr[2];

    A& operator=( A const& );       // No such.

    static Array<int, 2> const& oneAndTwo()
    {
        static Array<int, 2> const a = {1, 2};
        return a;
    }

public:
    A(): arr_( oneAndTwo() ) {}
    int at( Index i ) const { return arr_.elem[i]; }
};


int main()
{
    using namespace std;

    A o;
    for( int i = 0;  i < 2;  ++i )
    {
        cout << o.at( i ) << endl;
    }
}
你爱我像她 2024-12-22 23:29:16

将数组元素初始化为非零值需要 C++11 支持。

在 C++03 中,只能对数组进行值初始化,从而导致每个元素的值为 0

class A {
    const int arr[2];
public:
    A() : arr() { }
};

有关相关的 C++03 标准,请参阅此问题和答案:
我如何使用成员初始化列表来初始化它?

(我假设通过 C++ 98 你的意思是不是 C++11,即C++03 是可以接受的。如果这个假设是错误的,请指出。)

Initializing array elements to non-zero values requires C++11 support.

In C++03, it's only possible to value-initialize your array, resulting in each element's value being 0:

class A {
    const int arr[2];
public:
    A() : arr() { }
};

For the relevant C++03 standardese, see this question and answer:
How can i use member initialization list to initialize it?

(I'm going to assume that by C++98 you mean not C++11, i.e. that C++03 is acceptable. If this assumption is wrong, please say so.)

时光无声 2024-12-22 23:29:16

不,不是。

No. It's not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文