简体   繁体   English

C 是否有版本 JavaScript “这个”?

[英]Does C have a version of JavaScript "this"?

I've use quite a bit of JavaScript so far.到目前为止,我已经使用了相当多的 JavaScript。 If you were to use an object constructor in JavaScript, you have access to the this constructor.如果要在 JavaScript 中使用 object 构造函数,则可以访问this构造函数。

So my question relates to trying to use a similar concept in C.所以我的问题与尝试在 C 中使用类似的概念有关。 I created a struct that I want to be able to self reference:我创建了一个我希望能够自我引用的结构:

struct Storage {
    void (*delete)();
}

So if I were to allocate a Storage class:因此,如果我要分配一个Storage class:

struct Storage *myStruct = malloc(sizeof(struct Storage));

Let's say I'm trying to delete myStruct .假设我正在尝试删除myStruct If I have some delete function that I point to (with myStruct->delete = deleteStructure ), I would like to do something like this:如果我有一些我指向的delete function (使用myStruct->delete = deleteStructure ),我想做这样的事情:

myStruct.delete();

which would then free() the struct through a self referencing variable inside of said delete function.然后,它将通过所述delete function 内部的自引用变量free()结构。 I'm wondering if there would be a way to have the delete function look like:我想知道是否有办法让delete function 看起来像:

void deleteStructure() {
    free( /* "this" or some equivalent C self-reference */ );
}

My assumption from research so far is that this is not possible since this is usually only in object oriented programming languages.到目前为止,我的研究假设是这是不可能的,因为this通常只在面向 object 的编程语言中出现。 If this is not possible, I'm wondering what would be the semantically correct way to do this.如果这是不可能的,我想知道在语义上正确的方法是什么。 I'm hoping to make the usage of this delete functionality rather simplistic from a user interface perspective.我希望从用户界面的角度来看,这个delete功能的使用相当简单。 The only way I understand this to work would be passing a reference to the structure like:我理解这个工作的唯一方法是传递对结构的引用,例如:

void deleteStructure(struct Storage *someStructure) {
    free(someStructure);
}

which would then require deletion to be done as follows:然后需要按如下方式进行删除:

deleteStructure(myStruct);

To sum up: is there a way to make a delete function that uses self references in C, and if not, what would be the most semantically correct way to delete a structure in the most user friendly way?总结一下:有没有办法在 C 中使用自引用来删除 function,如果没有,以最用户友好的方式删除结构的最语义正确的方法是什么?

No. You cannot even define a function for a struct.不,您甚至不能为结构定义 function。

struct Storage {
    void (*delete)();
}

simply stores a pointer to a void function.简单地存储一个指向 void function 的指针。 That could be any void function and when it is being called, it has no connection to Storage whatsoever.那可能是任何void function 并且当它被调用时,它与Storage没有任何连接。

Also note that in your code, every instance of the struct stores one pointer to a void function.另请注意,在您的代码中,结构的每个实例都存储一个指向 void function 的指针。 You could initialize them so that they all point to the same function, in which case you would simply waste 64 bit per instance without any real benefit.您可以初始化它们,使它们都指向同一个 function,在这种情况下,您只会浪费每个实例 64 位而没有任何实际好处。 You could also make them point to completely different functions with different semantics.您还可以使它们指向具有不同语义的完全不同的函数。

As per @UnholySheep's comment, the correct semantical use of a struct with connection to a C function will follow the structure:根据@UnholySheep 的评论,与 C function 连接的结构的正确语义使用将遵循以下结构:

struct Storage {
    /* Some definitions here */
}

void deleteStructure(struct Storage *someStructure) {
    free( /* all inner structure allocations */ );

    free(someStructure);
}

Here's more about passing structs by reference. 这里有更多关于通过引用传递结构的信息。

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

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