在C静态结构的成员变量 [英] The member variable of a static struct in C

查看:141
本文介绍了在C静态结构的成员变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态的结构在C语言中的成员变量的问题。

I have a question about the member variables of static struct in C language.

有人说,我们可以声明一个静态结构,但在C,结构不具备静态成员像C ++类,这是什么意思?如果我宣布一个静态的结构,什么是成员的状态变量?可以有人帮助我在这?

Someone said we can declare a static struct, but in C, struct do not have the static members like class in C++, what does this mean? If I declare a static struct, what is the status of the members variable? can some one help me on this?

推荐答案

需要注意的是一个静态的结构本身是从结构的静态成员不同。虽然你可以声明静态结构变量:

Note that a static struct itself is different from a static member of a struct. While you can declare a static struct variable:

static struct MyStruct s;

您不能用静态成员定义一个结构类型:

you can't define a struct type with a static member:

struct MyStruct {
    static int i; // <- compiler error
};

这样做的原因是,在C,一个结构是一类 - (。即​​静态)声明一个类型,它的成员变量始终是相同的实例该类型的多个实例简直是痴人说梦。在C ++中,结构是现实类(它们只在成员的默认可见范围不同),以及C ++中的static关键字意味着在这种情况下别的东西。这意味着一个类的方法 - 但由于C没有类和方法,这是没有意义的,是无效的用C

The reason for this is that in C, a struct is a type - declaring a type of which a member variable is always the same instance (i. e. static) for multiple instances of that type is simply nonsense. In C++, structs are in reality classes (they only differ in the default visibility scope of members), and in C++ the static keyword means something else in this case. It means a class method - but since C doesn't have classes and methods, this doesn't make sense and is invalid in C.

经验教训:C ++不是C

Lesson learned: C++ is not C.

这篇关于在C静态结构的成员变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆