简体   繁体   English

面向对象的 C 类代码 - 不带参数的调用者识别

[英]Object Oriented C like code - Identification of caller without parameters

If I have this setup:如果我有这个设置:

#include <stdlib.h>
#define NEW_FOO ((foo_t*)malloc(sizeof(foo_t)))

void foo_func(void);

typedef struct {
    void (*foo) (void);
} foo_t;

int main(void) {
    foo_t *a = NEW_FOO;
    foo_t *b = NEW_FOO;
    a->foo = foo_func;
    b->foo = foo_func;
    a->foo();
    b->foo();
}

void foo_func(void) {
    // determine wheter a or b was called?
}

Can I then find out, wheter a or b was the caller of foo_func , strictly without a parameter like self , this , ... ?然后我能找出a还是bfoo_func的调用者,严格来说没有像selfthis...这样的参数吗?

The return address should be on the stack, so you should be able to identify the caller somehow, no?返回地址应该在堆栈上,所以你应该能够以某种方式识别调用者,不是吗?

I thought of a possible approach (it builds upon the idea above): The first time the foo_func is called (maybe through an initialization function, but let's leave that out to keep it simple) through a->foo() , store the address of struct a in some sort of array of pointers, I would assume.我想到了一种可能的方法(它建立在上述想法的基础上):第一次调用foo_func (可能通过初始化函数,但为了简单foo_func ,我们将其省略),通过a->foo()存储地址struct a在某种类型的指针数组中,我会假设。 Same with b->foo() .b->foo() Then, anytime that a->foo() or b->foo() is called, you would compare the address of the caller struct with the contents in the array to identify wheter it was a or b that called foo_func() .然后,无论何时a->foo()b->foo()被调用,您都会将调用者结构的地址与数组中的内容进行比较,以确定调用foo_func()a还是b

It's just that I have no Idea if and/or how that is possible, so if anyone of you could help me with this, I would be very glad!只是我不知道这是否和/或如何可能,所以如果你们中的任何人可以帮助我,我会很高兴!

I guess you're annoyed about the unsightliness of constructions like:我猜您对以下结构的难看感到恼火:

a->foo (a, arg0, arg1);
b->bar (b, arg0);

Unfortunately, the style of programming you've adopted does force this style on you, if you want to implement a simulation of polymorphic methods.不幸的是,如果您想实现多态方法的模拟,您所采用的编程风格确实会迫使您采用这种风格。 Maybe you can implement a set of macros so you can write something like:也许您可以实现一组宏,以便您可以编写如下内容:

METHOD_CALL2 (foo, a, arg0, arg1);
METHOD_CALL1 (bar, b, arg0);

and so not have to repeat the "object" names a , b , etc., in the call.因此不必在调用中重复“对象”名称ab等。 I've seen this done as well but, in my view, it doesn't look any prettier, and I'm sure it's no more maintainable.我也看到这样做了,但在我看来,它看起来并不漂亮,而且我确信它不再具有可维护性。

As this is C, not C++, in the end you're going to have to have some way to pass your equivalent of this to the "methods" in your implementation.由于这是C,而不是C ++,到底你要必须有一些方法来你相当于通过this在您执行“办法”。 You might be able to disguise it with macros and variable-length argument lists, but it's going to have to happen somehow.您也许可以用宏和可变长度参数列表来掩饰它,但它必须以某种方式发生。

But why worry?但是为什么要担心呢? This is idiomatic C code -- every application and library that takes an object-oriented approach to C will be using constructions of the form you want to avoid.这是惯用的 C 代码——每个采用面向对象的 C 方法的应用程序和库都将使用您想要避免的形式的构造。 People will understand what you're doing.人们会明白你在做什么。 Trying to disguise it will not make your code easier to follow, I suspect.我怀疑,试图伪装它不会使您的代码更容易理解。

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

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