[英]How can i make this change on C function struct?
我一直在嘗試在 C 上模擬 Python 實例構造,但我找不到將結構名稱向后獲取到 ZC1C425268E68385D1AB5074C17A94F14 的方法
typedef struct queue {
.
.
ssize_t (*add)(struct queue *, int);
} queue;
ssize_t add(queue *self, int value)
{
/* Add a element to the queue */
}
int main(void)
{
queue *clients = createQueue(5); /* Here i create the queue struct */
clients.add(5); /* I want to convert this instruction to "clients.add(&clients, 5);" */
}
我試過使用宏,但我發現我不能使用正則表達式或類似的東西
用非 object 語言(如 C)很難“模擬” object 語言,但在 ZF6F877C9FDCF18B666Z 語言中很容易做到
struct queue { // a 'struct' is a 'class' where all is 'public' by default
.
.
ssize_t add(int); // in C++ 'this' corresponding to 'self' does not have to be explicitly placed in the params list
};
ssize_t queue::add(int value)
{
/* Add a element to the queue */
return ...a ssize_t...;
}
int main(void)
{
queue * clients = createQueue(5); /* Here i create the queue struct */
clients->add(5); // '->' rather than '.' because clients is a 'queue*' rather than a 'queue'
}
Python 'self' 的對應在 C++ 中被命名為 'this' 並且您不必將其顯式放置在參數列表中,它是隱式的。
注意在 C++ 中沒有像 Python 中那樣的垃圾收集器,可能createQueue
在堆中分配隊列,在這種情況下,如果您想釋放分配的 ZCD69B4957F06CD818D7BF3D61980E21 執行結束之前必須執行delete queue;
最后還有一個隱式的return 0;
如果您不自己返回,則在main結束時
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.