简体   繁体   English

C中的对象-结构语法相关

[英]Objects in C - Structs Syntax Related

I was recently reading about ways to create something that resembles c++ objects, but in C, and I came across some pretty good examples. 我最近在阅读有关创建类似于c ++对象的方法的信息,但是在C语言中,我遇到了一些非常好的示例。 However, there was this single piece of code that had me thinking for hours, since it was the first time I saw this kind of syntax, and I didn't find anything like it on Google... 但是,这是我想了好几个小时的代码,因为这是我第一次看到这种语法,而且我在Google上找不到类似的东西...


The block itself is this one: 块本身就是这个:

struct stack {
     struct stack_type * my_type;
     // Put the stuff that you put after private: here
};
struct stack_type {
     void (* construct)(struct stack * this); // This takes uninitialized memory
     struct stack * (* operator_new)(); // This allocates a new struct, passes it to construct, and then returns it
     void (*push)(struct stack * this, thing * t); // Pushing t onto this stack
     thing * (*pop)(struct stack * this); // Pops the top thing off the stack and returns it
     int this_is_here_as_an_example_only;
}Stack = {
    .construct = stack_construct,
    .operator_new = stack_operator_new,
    .push = stack_push,
    .pop = stack_pop
};

Assuming all the functions being set to the pointers are defined somewhere else, my doubts are the following: 假设所有设置为指针的函数都在其他位置定义,我的疑虑如下:

1) Why does the point ( '.' ) mean, or whats its purpose when initializing the function pointers? 1)为什么在初始化函数指针时,点( '。' )是什么意思? (example: .construct = stack_construct ) (例如: .construct = stack_construct

2) why is there an equal sign after 'Stack' at the end of the struct definition, and why is there something other than a ';' 2)为什么在结构定义末尾的“堆栈”后面有一个等号,以及为什么除了“;”之外还有其他东西 ( in this case the word 'Stack' ), given the fact that there is no typedef at the beginning? (在本例中为“ Stack”一词),考虑到一开始就没有typedef I assume it has something to do with initialization (like a constructor, I don't know), but it is the first time I see a struct = {...,...,...} in the definition. 我认为它与初始化有关(我不知道像构造函数一样),但这是我第一次在定义中看到struct = {...,...,...}。 I've seen that when you initialize a struct like in the following example: 我已经看到,当您初始化以下示例中的结构时:

typedef struct s{
int a;
char b;
}struc;

void main(){
    struc my_struct={12,'z'};
}

But this is not in the main declaration of the struct, and still, there are no '=' within the {} , unlike the first example, where it showed something like... 但这不在结构的主声明中,并且在{}中仍然没有'=' ,这与第一个示例不同,在第一个示例中它显示类似...

struc my_struct={ a = 12,
                  b = 'z'};

3) This is a minor doubt, meaning, I'm much more interested in the first two. 3)这是一个小疑问,这意味着,我对前两个更感兴趣。 Anyway, here it goes... At the beginning of the first code, it says something like '// Put the stuff that you put after private: here'. 无论如何,它就开始了……在第一个代码的开头,它说了类似“ //将您放置在private之后的内容:这里”。 Why is that? 这是为什么? how would that make them private? 如何将它们设为私有?


That is all, I would appreciate anything, this had me thinking for hours! 仅此而已,我将不胜感激,这让我想了好几个小时! Thanks in advance, have a great day! 在此先感谢您,祝您度过愉快的一天!

1) Don't worry about the members being functions, it's just this: 1)不用担心成员是函数,仅此而已:

What does dot (.) mean in a struct initializer? 点(。)在结构初始化程序中是什么意思?

2) In 2)在

struct S {int i;}
s = {0};

the first line names a type that can be used to declare and initilize a variable. 第一行命名一个可用于声明和初始化变量的类型。

It is really equivalent with something like 真的等同于

double d = 1;

where you replace double with struct S {int i;}, replace s with d, and replace 1 with the initializer for a struct, {0}. 在其中,将double替换为struct S {int i;},将s替换为d,并将1初始化为结构体{0}的初始化程序。 Now combine this with the dot syntax. 现在,将其与点语法结合起来。

It just has the side effect of also defining struct S. 它也具有定义结构S的副作用。

Update: Regarding 3), I do not think that with private they refer to an implementation of access control, but rather refer to the fact that in C++, you would usually list the data members in the private section of the class, so I understand this as instructions to add the data members after the type identifying element. 更新:关于3),我不认为私有是指访问控制的实现,而是指这样的事实,即在C ++中,您通常会在类的私有部分中列出数据成员,因此我理解这作为在类型标识元素之后添加数据成员的说明。

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

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