简体   繁体   English

使用 C 中的结构对相关常量进行分组是一种好习惯吗?

[英]Is it a good practice to group related constants using structs in C?

I was wondering if it would be a good idea to use structs as pseudo namespaces (à la C++) to group constants which are functionally or conceptually related to each other.我想知道将结构用作伪名称空间(à la C++)来对在功能上或概念上彼此相关的常量进行分组是否是个好主意。

static const struct {

    const unsigned int START;
    const unsigned int END;

} COUNTER = {.START = 1, .END = 100};

Is there any downside to this?这有什么缺点吗? If not, is it redundant (or maybe even unconvenient) to have both the struct instance and its members declared as const ?如果不是,将 struct 实例及其成员都声明为const是否是多余的(甚至可能不方便)? Where should the constantness of these values be stated?应该在哪里说明这些值的恒定性?

I was wondering if it would be a good idea to use structs as pseudo namespaces我想知道将结构用作伪名称空间是否是个好主意

Well, it CAN be a good idea.好吧,这可能是个好主意。 It's not intrinsically bad.它本质上并不坏。 An argument against is that if you feel that you need namespaces, then it's likely that C is the wrong language in the first place.反对的一个论点是,如果您觉得需要命名空间,那么 C 很可能首先是错误的语言。 But it can be used this way, and it is sometimes used this way.但是可以这样使用,有时也这样使用。

Where should the constantness of these values be stated?应该在哪里说明这些值的恒定性?

It's in general enough to declare the whole struct as const.一般来说,将整个结构声明为 const 就足够了。 But beware with pointers.但要小心指针。 This code is valid and will print "42":此代码有效,将打印“42”:

int x = 5;

const struct {
        int *p;
} S = {.p = &x };

int main()
{
        *(S.p) = 42;
        printf("%d\n", x);
}

In the above code, you are not allowed to change Sp so that it points to something else, but there is a difference between a const pointer and a pointer to const.在上面的代码中,你不能改变 Sp 使它指向别的东西,但是 const 指针和指向 const 的指针是有区别的。 So for pointer, it could be a good idea to add an extra const.所以对于指针,添加一个额外的常量可能是个好主意。

To clarify, the pointer p will be declared like it was a int * const p which means you cannot change the pointer itself, but in order to protect the data it's pointing to, you need const int *p .为了澄清,指针p将被声明为int * const p这意味着您不能更改指针本身,但为了保护它指向的数据,您需要const int *p To get both, use const int * const p , but if the struct is declared as const, you'll get one of them "for free" so const int *p is enough to get both.要同时获得两者,请使用const int * const p ,但如果将结构声明为 const,您将“免费”获得其中之一,因此const int *p足以获得两者。

And if you consider pointers to pointers, well, think it through for a long time and test it to make sure it works the way you want.而且,如果您考虑指向指针的指针,那么请考虑很长时间并对其进行测试以确保它以您想要的方式工作。

From comments:来自评论:

Why not enums?为什么不是枚举?

Because this is not valid:因为这是无效的:

enum S {a = 5};
enum Y {a = 6};

The compiler will tell you that a is already defined.编译器会告诉你a已经定义了。 So enums is not good for emulating namespaces.所以枚举不适合模拟命名空间。 Also, you cannot use enums for non-integers.此外,您不能将枚举用于非整数。

Is it a good practice to group related constants using structs in C?使用 C 中的结构对相关常量进行分组是一种好习惯吗?

It's opinion based.它是基于意见的。 If it works for you, do it.如果它对你有用,那就去做吧。

I wouldn't do like that i C.我不会那样做,我 C。 Instead I use #define相反,我使用#define

Like:喜欢:

#define GROUPNAME_NAME

so in your case I would do所以在你的情况下我会做

#define COUNTER_START 1
#define COUNTER_END 100

In C++ I would do:在 C++ 我会这样做:

const unsigned int COUNTER_START = 1;
const unsigned int COUNTER_END = 100;

The difference between C and C++ is due to differences in language specification. C 和 C++ 之间的差异是由于语言规范的差异。

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

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