C语言 C 中的“this”指针(不是 C++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4442580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"this" pointer in C (not C++)
提问by foo
I'm trying to create a stack in C for fun, and came up with the idea of using struct to represent the stack. Then I add function pointers to the struct for push() and pop() operations.
我试图在 C 中创建一个堆栈以获得乐趣,并提出了使用 struct 来表示堆栈的想法。然后我将函数指针添加到用于 push() 和 pop() 操作的结构中。
So far all is good it seems, but, for the implementation of the push() and pop() functions I need to refer to *this somehow. How can that (can it?) be done?
到目前为止,一切似乎都很好,但是,对于 push() 和 pop() 函数的实现,我需要以某种方式引用 *this。这(可以吗?)怎么做?
This is my struct
这是我的结构
struct Stack {
int *data;
int current_size;
int max_size;
int (*push)(int);
int (*pop)();
};
And as an example here's push
作为一个例子,这里是 push
int push(int val) {
if(current_size == max_size -1)
return 0;
data[current_size] = val;
current_size++;
return 1;
}
As you can imagine, the compiler has no idea what current_sizeis, as it would expect something like stack->current_size.
可以想象,编译器不知道是什么current_size,因为它期望像stack->current_size.
Is this possible to overcome somehow?
这有可能以某种方式克服吗?
回答by NPE
There's no implicit thisin C. Make it explicit:
thisC 中没有隐含的。让它明确:
int push(Stack* self, int val) {
if(self->current_size == self->max_size - 1)
return 0;
self->data[self->current_size] = val;
(self->current_size)++;
return 1;
}
You will of course have to pass the pointer to the struct into every call to pushand similar methods.
您当然必须将指向结构的指针传递到每次调用push和类似方法中。
This is essentially what the C++ compiler is doing for you when you define Stackas a class and pushet al as methods.
这本质上就是 C++ 编译器在您定义Stack为类并定义push为方法时为您做的事情。
回答by Victor Nicollet
The typical approach in C is to have functions expect thisas the first parameter.
C 中的典型方法是将函数this作为第一个参数。
int push(Stack *self, int val)
{
if (self->current_size == self->max_size -1) return 0;
self->data[self->current_size++] = val;
return 1;
}
This has the added benefit that, unless you need polymorphism, you don't need to put the functions in the stack, because you could just call push(stack, 10)instead of stack->push(stack,10).
其优点是,除非你需要多态,你不需要把函数栈,因为你可以只调用额外的好处push(stack, 10),而不是stack->push(stack,10)。
回答by Tyler Eaves
C doesn't work like that. It's not an object oriented language. Functions that manipulate data structures need to take a pointer to the structure as an argument.
C 不是那样工作的。它不是面向对象的语言。操作数据结构的函数需要将指向该结构的指针作为参数。
回答by Niki Yoshiuchi
Your function pointers aren't methods so they don't have any information about the calling object. The only way to do what you want is to either pass in a pointer to the object, or make that pointer global (the latter is not recommended).
您的函数指针不是方法,因此它们没有关于调用对象的任何信息。做你想做的唯一方法是传递一个指向对象的指针,或者将该指针设为全局(不推荐后者)。
回答by ThomasMcLeod
Obviously you can have a Stack * member in the struct and then just initialize it with the address of the struct before you use the function pointers. Then make the Stack * a parameter on the function pointers.
显然你可以在结构中有一个 Stack * 成员,然后在使用函数指针之前用结构的地址初始化它。然后使 Stack * 成为函数指针上的参数。
回答by karlphillip
Since your are going to have only one Stackstructure (that you named stack, apparently), you could define it as a global variable. This would allow pop/push to refer to the stackvariable directly.
由于您将只有一个Stack结构(显然您将其命名为stack),因此您可以将其定义为全局变量。这将允许 pop/push 直接引用堆栈变量。
You would do something like:
你会做这样的事情:
stack.current_size += 4;
stack.current_size += 4;
or use the ->operator if you decide to declare stackas a memory pointer to Stack.
或者,如果您决定将stack声明为指向Stack的内存指针,请使用->运算符。

