简体   繁体   English

类内部的C ++常量结构初始化

[英]C++ constant structure initialization inside a class

How should I write a constructor for a class to initialize a member that is a const structure / has const fields? 我应该如何为一个类编写一个构造函数以初始化一个具有const结构/具有const字段的成员?

In the following example, I define a constructor within structure B and it works fine to initialize it's const fields. 在以下示例中,我在结构B中定义了一个构造函数,并且可以很好地初始化其const字段。

But when I try to use the same technique to initialize const fields of structure C within class A it doesn't work. 但是,当我尝试使用相同的技术在类A中初始化结构C的const字段时,它不起作用。 Can someone please help me and rewrite my class A in a way, that it starts working? 有人可以帮助我,并以某种方式重写我的A类,使其开始起作用吗?

#include <iostream>
class A
{
public:
    struct C
    {
            C (const int _x) : x (_x) {}
            const int x;
    };

    C c (3);

};
int main (int argc, char *argv[])
{
struct B
{
    B (const int _x) : x (_x) {}
    const int x;
};

B b (2);
std::cout << b.x << std::endl;

A a;
std::cout << a.c.x << std::endl;

return 0;
}

PS PS

  1. I did some search and I think, I understand, that unless I have C++11 support or want to use boost library, I have to define a helper function to initialize a const struct within initialization list ( C++ Constant structure member initialization ) but it seems to be crazy that I have to define alike struct, but with non const fields to initialize a struct with const fields, doesn't it? 我进行了一些搜索,据我所知,除非我有C ++ 11支持或想使用Boost库,否则我必须定义一个辅助函数来初始化初始化列表中的const结构( C ++常数结构成员initialize ),但是我必须定义一个相似的结构似乎很疯狂,但是使用非const字段来初始化带有const字段的结构,不是吗?

  2. Another thing that I found tells that I should initialize const members in a constructor of the class A, rather than in a constructor of the struct C ( C++ compile time error: expected identifier before numeric constant ) but it also seems crazy to me, because why should I rewrite a class constructor every time I want to add a new struct, isn't it more convenient to have a separate constructor for each struct C within the class A? 我发现的另一件事告诉我,我应该在A类的构造函数中初始化const成员,而不是在struct C的构造函数中初始化( C ++编译时错误:数字常量之前的预期标识符 ),但对我来说,这似乎也很疯狂,因为为什么每次我想添加一个新结构时都应该重写一个类构造函数,为类A中的每个结构C分别设置一个构造函数不是更方便吗?

I would be grateful to any comments that could possibly clarify my confusion. 我很感谢任何可能澄清我的困惑的评论。

I'd do the job like this: 我会做这样的工作:

#include <iostream>
class A {
public:
    struct C {
        C(const int _x) : x(_x) {}
        const int x;
    };

    C c; // (3);
    A() : c(3) {}
};
int main(int argc, char *argv []) {
    A a;
    std::cout << a.c.x << std::endl;

    return 0;
}

Note that it's not a matter of using a ctor in A or in C , but of the ctor for A telling how the ctor for C should be invoked. 请注意,这不是在A C中使用ctor的问题,而是A的ctor告诉应如何调用C的ctor的问题。 If the value that will be passed will always be 3 that's not necessary, but I'm assuming you want to be a able to pass a value of your choice when you create the C object, and it will remain constant after that. 如果将要传递的值始终为3 ,则没有必要,但是我假设您希望能够在创建C对象时传递您选择的值,并且此后它将保持不变。

If the value will always be the same ( 3 in this case) you can simplify things a lot by also making the constant static : 如果该值始终相同(在这种情况下为3 ),则可以通过使常量为static来简化很多事情:

struct A {
    struct C {
        static const int x = 3;
    };
    C c;
};

int main() { 
    A a;
    std::cout << a.c.x << "\n";
}

So, if the value is identical for all instances of that class, make it static const , initialize it in place, and life is good. 因此,如果该类的所有实例的值都相同,则将其static const ,就地对其进行初始化,这样一来,生命就很好了。 If the value is not known until you create an instance of the object, and remains constant thereafter for the life of that object, you need to pass it in through the constructors. 如果在创建对象实例之前未知该值,并且此后在该对象的生存期内保持不变,则需要将其传递给构造函数。

For a slightly different case, there's a third possibility: if C is an independent class (not nested inside of A ) you might have a situation where other instances of C use various values, but all instances of C inside an A always use the same value. 对于稍微不同的情况,还有第三种可能性:如果C是一个独立的类(未嵌套在A ),则可能会遇到C其他实例使用各种值,但A内的所有C实例始终使用相同值的情况。值。 In this case, you'd do something like: 在这种情况下,您可以执行以下操作:

struct C { 
    const int x;

    C(int x) : x(x) {}
};

struct A { 
     C c;

     A() : c(3) {}
};

Of course, you can do the same thing when C is nested inside of A , but when/if you do, it generally means you're setting the same value for all instances of C , so you might as well use the static const approach instead. 当然,当C嵌套在A内时,您可以执行相同的操作,但是当/如果这样做,则通常意味着您要为C所有实例设置相同的值,因此您最好使用static const方法。代替。 The obvious exception would be if A had multiple constructors, so (for example) A 's default constructor passed one value for C::x and its copy constructor passed a different value. 一个明显的例外是,如果A有多个构造函数,那么(例如) A的默认构造函数为C::x传递一个值,而其副本构造函数传递一个不同的值。

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

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