简体   繁体   English

是否允许结构的成员是静态的?

[英]Are members of a structure allowed to be static ?

#include<stdio.h>
struct str 
{ 
   static int a ;
    int b ;
 } s ;
int main()
{
static int p , k ;
printf("%d %d",sizeof(p),sizeof(s));
getchar();
return 0;
}

above code is giving errors . 上面的代码给出了错误。 But if I redefine the first member of the structure to 'int' rather than 'static int' then it runs fine . 但是,如果我将结构的第一个成员重新定义为'int'而不是'static int',则它可以正常运行。 Why static members are not allowed in the structure and what is its significance ? 为什么在结构中不允许使用静态成员,其意义是什么?

There's simply no such feature in C language. C语言根本没有这样的功能。 And there's no meaningful conceptual framework for such feature in C. 而且,C中没有针对此类功能的有意义的概念框架。

You see, in C++ terms, there's only one relevant difference between a static member variable and an ordinary global variable: the scope in which its name is declared and the corresponding naming syntax. 您可以看到,用C ++术语来说, static成员变量和普通全局变量之间只有一个相关的区别:声明其名称的范围和相应的命名语法。 A global variable could be called a , while a static member of the class would be called SomeClass::a . 全局变量可以称为a ,而类的静态成员称为SomeClass::a Besides scoped naming, there are no other differences. 除了作用域命名之外,没有其他区别。 (I deliberately ignore other C++-specific features, like access control, since they don't exist in C and this question is really about C.) (我故意忽略其他特定于C ++的功能,例如访问控制,因为它们不存在于C中,并且这个问题确实与C有关。)

In C language a struct type does not introduce its own scope. 在C语言中,结构类型不会引入自己的范围。 There's no such naming syntax as SomeStruct::a in C language. C语言中没有诸如SomeStruct::a这样的命名语法。 For this reason there's simply no reason to have static members in structs. 因此,根本没有理由在结构中包含静态成员。 You can declare a global variable instead and achieve the same effect. 您可以改为声明全局变量并获得相同的效果。 Call your global variable str_a to convey the intent to "associate" it with struct str and just think of that variable as a pseudo-static member of struct str . 调用全局变量str_a来传达将其与struct str “关联”的意图,并仅将该变量视为struct str的伪静态成员。

Formally speaking, one could do it the same way in C++, ie completely ignore this feature of C++ language and use global functions and variables instead of static function and variables inside classes. 从形式上来讲,可以在C ++中实现相同的功能,即完全忽略C ++语言的这一功能,并使用全局函数和变量而不是类中的静态函数和变量。 However, by doing that one would forsake all member access control features of C++. 但是,这样做将放弃C ++的所有成员访问控制功能。 And these features are really worth having. 这些功能确实值得拥有。 C language has no access control features, meaning that in C one loses [almost] nothing. C语言没有访问控制功能,这意味着C语言几乎不会丢失任何东西。

The language just doesn't allow it. 语言只是不允许。 There's no deeper reason other than that it's not part of the design. 除了它不是设计的一部分之外,没有其他更深层次的原因。 You can always achieve the same behaviour with a separate global variable like this: 您始终可以使用单独的全局变量来实现相同的行为,如下所示:

struct str
{
    int b;
} s;

int str_a;

Note that it would be something entirely different to have a non -static int a; 注意,拥有一个静态的int a;将会完全不同int a; inside your struct, which would be a distinct subelement of every object of type struct str . 在您的struct内部,这将是struct str类型的每个对象的唯一子元素。

(Note also that in C++, a language evolved from C, static class members do exist and behave exactly like the workaround I described above, only that the name of the global variable is tightly associated to the name of the class.) (还请注意,在C ++中,一种从C演变而来的语言,确实存在静态类成员,并且其行为与我上述的解决方法完全相同,只是全局变量的名称与类的名称紧密相关。)

A static modifier is to declare your variable in the global scope in your file and a static modifier in a function creates a variable with a persistant value limited to the scope of this functions. 静态修饰符是在文件的全局范围内声明变量,而函数中的静态修饰符会创建一个变量,其持久性值限于此函数的范围。 And you can not share the value of this integer between your instances of your struct. 而且,您不能在结构实例之间共享此整数的值。 This is not and cannot be supported in C ;) 这不是,也不能在C中支持;)

Why do you want to use a static member in a struct? 为什么要在结构中使用静态成员? maybe there is (there must be) a better soluation. 也许(必须)有更好的解决方案。

No, not in C. I believe C++ can do this and it means there is one copy of a that is shared amongst all instances of the struct str structure. 不,不是在C中。我相信C ++可以做到这一点,这意味着在struct str结构的所有实例之间共享a 一个副本。

If you want to do something similar in C, you have a few options (there may be more, I just can't think of them at the moment). 如果您想在C语言中执行类似的操作,则有几种选择(可能还有更多选择,我暂时无法想到它们)。

The first is to break out the common variable with something like: 首先是使用类似以下内容的通用变量:

int struct_str_static_a;
struct str {
    int b;
} s;

That way, there is only one copy of a shared by all instances of the structure - each instance still gets its own copy of b . 这样一来,只有一个拷贝a通过结构的所有实例共享-每个实例仍然得到了自己的副本b

A slight modification to that is to introduce a pointer to that common variable and initialise the pointer: 对它的一个小修改是引入一个指向该公共变量的指针并初始化该指针:

int struct_str_static_a;
struct str {
    int *pA;
    int b;
} s;
:
s.pA = &struct_str_static_a;

Then you can use *(s.pA) where before you would have used sa . 然后,可以在使用sa之前使用*(s.pA) Because every instance of struct str has its own pA pointer that points to a single a , that gives you a similar effect. 因为struct str每个实例都有其自己的pA指针,该指针指向单个a ,所以可以得到类似的效果。 However, it's a torturous road to follow. 但是,这是一条曲折的道路。

The third option is to get yourself on the next ISO C working group and put this forward as a change to the language. 第三种选择是让自己进入下一个ISO C工作组,并将其作为对语言的更改提出。 However, that's going to require a fair bit of effort from yourself for the next ten years or so, probably not worth the effort :-) 但是,在接下来的十年左右的时间里,这需要您付出相当大的努力,可能不值得付出努力:-)

You have good answers here: http://cboard.cprogramming.com/c-programming/123691-static-variable-structure.html 您在这里有很好的答案: http : //cboard.cprogramming.com/c-programming/123691-static-variable-structure.html

generally speaking you don't have any gain from declaring it static, but if you still wish to it , you may migrate to c++ or declare the whole struct as static. 一般而言,将其声明为静态没有任何好处,但是如果您仍然希望将其声明为静态,则可以迁移到c ++或将整个结构声明为静态。

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

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