如何实现 C++ C中的虚函数

发布于 2024-09-07 02:54:41 字数 59 浏览 7 评论 0原文

C++ 语言提供虚拟 函数。在纯C语言实现的限制下,如何才能达到类似的效果呢?

The C++ language provides virtual functions. Within the constraints of a pure C language implementation, how can a similar effect be achieved?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沉默的熊 2024-09-14 02:54:41

盗自此处

从 C++ 类

class A {
protected:
    int a;
public:
    A() {a = 10;}
    virtual void update() {a++;}
    int access() {update(); return a;}
};

可以派生出 C 代码片段。 A 类 的三个 C++ 成员函数使用外联(独立)代码重写,并按地址收集到名为 A_functable 的结构中。 A 的数据成员与函数表组合成一个名为 A 的 C 结构体。

struct A;

typedef struct {
    void (*A)(struct A*);
    void (*update)(struct A*);
    int (*access)(struct A*);
} A_functable;

typedef struct A{
    int a;
    A_functable *vmt;
} A;

void A_A(A *this);
void A_update(A* this);
int A_access(A* this);

A_functable A_vmt = {A_A, A_update, A_access};

void A_A(A *this) {this->vmt = &A_vmt; this->a = 10;}
void A_update(A* this) {this->a++;}
int A_access(A* this) {this->vmt->update(this); return this->a;}

/*
class B: public A {
public:
    void update() {a--;}
};
*/

struct B;

typedef struct {
    void (*B)(struct B*);
    void (*update)(struct B*);
    int (*access)(struct A*);
} B_functable;

typedef struct B {
    A inherited;
} B;

void B_B(B *this);
void B_update(B* this);

B_functable B_vmt = {B_B, B_update, A_access};

void B_B(B *this) {A_A(this); this->inherited.vmt = &B_vmt; }
void B_update(B* this) {this->inherited.a--;}
int B_access(B* this) {this->inherited.vmt->update(this); return this->inherited.a;}

int main() {
    A x;
    B y;
    A_A(&x);
    B_B(&y);
    printf("%d\n", x.vmt->access(&x));
    printf("%d\n", y.inherited.vmt->access(&y));
}

比必要的更详细,但它表达了要点。

Stolen from here.

From the C++ class

class A {
protected:
    int a;
public:
    A() {a = 10;}
    virtual void update() {a++;}
    int access() {update(); return a;}
};

a C code fragment can be derived. The three C++ member functions of class A are rewritten using out-of-line (standalone) code and collected by address into a struct named A_functable. The data members of A and combined with the function table into a C struct named A.

struct A;

typedef struct {
    void (*A)(struct A*);
    void (*update)(struct A*);
    int (*access)(struct A*);
} A_functable;

typedef struct A{
    int a;
    A_functable *vmt;
} A;

void A_A(A *this);
void A_update(A* this);
int A_access(A* this);

A_functable A_vmt = {A_A, A_update, A_access};

void A_A(A *this) {this->vmt = &A_vmt; this->a = 10;}
void A_update(A* this) {this->a++;}
int A_access(A* this) {this->vmt->update(this); return this->a;}

/*
class B: public A {
public:
    void update() {a--;}
};
*/

struct B;

typedef struct {
    void (*B)(struct B*);
    void (*update)(struct B*);
    int (*access)(struct A*);
} B_functable;

typedef struct B {
    A inherited;
} B;

void B_B(B *this);
void B_update(B* this);

B_functable B_vmt = {B_B, B_update, A_access};

void B_B(B *this) {A_A(this); this->inherited.vmt = &B_vmt; }
void B_update(B* this) {this->inherited.a--;}
int B_access(B* this) {this->inherited.vmt->update(this); return this->inherited.a;}

int main() {
    A x;
    B y;
    A_A(&x);
    B_B(&y);
    printf("%d\n", x.vmt->access(&x));
    printf("%d\n", y.inherited.vmt->access(&y));
}

More elaborate than necessary, but it gets the point across.

◇流星雨 2024-09-14 02:54:41

@GCC ....虚函数在对象的基类中声明,然后在子类中“覆盖”或实现。即,假设您有车辆基类,并且创建了两个子类:摩托车和汽车。基类将声明一个虚拟函数 AddTires() 然后子类将实现该函数,并且每个子类将以不同的方式实现它。汽车有 4 个轮子,而摩托车有 2 个轮子。不过,我无法为您提供 C 或 C++ 的语法。希望这有帮助

@GCC....A virtual function is declared in the Base class of an object and is then "overriden" or implemented in the sub classes. i.e., say you have Vehicle Base class and you create two sub-classes, Motorcycle and, Automobile. The Base class would declare a virtual function of AddTires() Then the Sub Classes would implement this function and each sub class would implement it differently. A car has 4 wheels, where a motorcycle has 2. I can't give you the syntax for C or C++, though. Hope this helps

も星光 2024-09-14 02:54:41

虚函数是C++面向对象的一个​​特性。它们指的是依赖于特定对象实例的方法,而不是您当前携带它们的类型。

换句话说:如果将一个对象实例化为 Bar,然后将其转换为 Foo,虚拟方法仍将是实例化时的方法(在 Bar 中定义),而其他方法将是来自 Foo 的方法。

虚拟函数通常通过 vtable 的方式实现(您需要对此进行更多研究;))。

您可以通过使用结构作为穷人的对象并在其中保存函数指针来在 C 中模拟类似的事情。

(更准确地说,非虚函数使得该方法应该从哪个类中获取变得不明确,但实际上我相信 C++ 使用当前类型。)

Virtual functions are a feature of C++'s object orientation. They refer to methods that depend on a specific object instance rather than what type you're currently carrying them around as.

In other words: if you instantiate an object as Bar, then cast it to Foo, virtual methods will still be the ones they were at instantiation (defined in Bar), while other methods will be the ones from Foo.

Virtual functions are typically implemented by way of vtables (that's for you to do more research on ;)).

You can simulate similar things in C by using structs as poor man's objects and saving function pointers in them.

(More correctly, non-virtual functions make it ambiguous which class the method should be taken from, but in practice I believe C++ uses the current type.)

暖树树初阳… 2024-09-14 02:54:41

这里是对虚拟函数的描述。

无法在普通 C 中实现虚函数,因为 C 没有继承的概念。

更新:
正如下面的评论中所讨论的,可以使用结构和函数指针来执行与直接 C 中的虚拟函数类似的操作。然而,如果您习惯了像 C++ 这样具有“真正的”虚函数的语言,您可能会发现 C 近似不太优雅且难以使用。

Here is a description of what virtual functions are.

There is no way to implement virtual functions in plain C, because C has no notion of inheritance.

Update:
As is discussed in the comments below, it is possible to do something similar to virtual functions in straight C using structures and function pointers. However, if you are accustomed to a language like C++ that has "true" virtual functions, you will probably find the C approximation far less elegant and harder to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文