c语言杂谈系列:模拟虚函数

从整体来看,笔者的做法与之前的模拟多态十分相似,毕竟c++多态的实现与虚函数密切相关

废话少说,see my code:

kernel.c

#include "kernel.h"
#include <stdio.h>

void shape_draw(struct shape_t* obj) {
    /* Call draw of the real Instance */


    obj->vtable->draw();

}

kernel.h:

#ifndef UNTITLED_KERNEL_H
#define UNTITLED_KERNEL_H


struct shape_t {
    /*Virtual Method Table */
    const struct shape_interface* const vtable;
};


struct shape_interface {

    void (*draw)();

};


void shape_draw(struct shape_t* obj);

    //obj->vtable->draw();

#endif //UNTITLED_KERNEL_H

try.c:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "try.h"


void draw() {
    printf("error is try!\n");
}

void draw1() {
    printf("error is try2!\n");
}


struct shape_t* shape_create_rectangle() {
	
	//直接赋值,这里有个命名错误,跟gcc有关:
    //static const struct shape_interface_t vtable = { draw1 } ;
    //static struct shape_t base = { &vtable };
    
    //笔者认为给结构体成员赋值,下面的写法更妥当
    
    static const struct shape_interface vtable = { 
            .draw = draw1
    } ;

    static struct shape_t base = { 
            .vtable = &vtable
    };
    //推荐上面这种写法,因为某些编译器很有趣

    struct rectangle_t* rectangle = malloc(sizeof(*rectangle));
    memcpy(&rectangle->base, &base, sizeof(base));

    return (struct shape_t*)(&rectangle->base);
}

顺便一提,clion的编译器相当有趣

笔者在之前曾经写错了shape_interface (_t)结构体名称,但是笔者发现:

//static const struct shape_interface_t *vtable = { draw1 } ;
//static struct shape_t base = { &vtable };

改成这样也能运行

这是为什么呢?笔者推测,gcc应该是无法找到对于结构体,就把vtable当成了数组,加上*就成为了数组。然后&vtable就成为了二级指针,由于draw1本身就是一个指针,把它转成空指针什么的可以随便赋值。gcc在找不到对应结构体后,索性为base里的vtable开辟了一段空间,由于&vatble是二级指针,但是找不到对应地址指向,可能它在编译过程中被转为了一级空指针,且等于draw1本身,这样就能解释通了。(如果有c语言高手可以留言解答一下,笔者对c语言和编译器的处理所知甚少)

try.h:

#ifndef UNTITLED_TRY_H
#define UNTITLED_TRY_H

#include "kernel.h"

struct rectangle_t {
    struct shape_t* base; /* Reference to Base Class */
    /* Rectangle specific Members */
    int x;
    int y;
};

struct shape_t* shape_create_rectangle();
        

#endif //UNTITLED_TRY_H

在主函数中这样调用即可:

main.c:

#include "try.h"
#include "kernel.h"




int main() {
    struct shape_t* rectangle = shape_create_rectangle();


    shape_draw(rectangle);


    return 0;
}

接下来是重点,虚函数表的实现,可以适当改动try.c文件:

try.c:

//
// Created by el on 2024/8/16.
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "try.h"


void draw() {
    printf("error is try!\n");
}

void draw1() {
    printf("error is try2!\n");
}


struct shape_t* shape_create_rectangle() {
	//static const struct shape_interface *vtable[] = { draw , draw1 } ;
	
	
    static const struct shape_interface_t *vtable[] = { 
    	.vtable = draw
    } ;

    static struct shape_t base = { vtable + 1};

    struct rectangle_t* rectangle = malloc(sizeof(*rectangle));
    memcpy(&rectangle->base, &base, sizeof(base));

    return (struct shape_t*)(&rectangle->base);
}

使用函数指针数组,就可以模拟出比较相近的虚函数表。

整个c程序的UML图如下:

其实这张图跟笔者前一篇模拟多态的文章思想是一样的。

相关推荐
o独酌o14 分钟前
递归的‘浅’理解
java·开发语言
Book_熬夜!16 分钟前
Python基础(六)——PyEcharts数据可视化初级版
开发语言·python·信息可视化·echarts·数据可视化
轩轶子44 分钟前
【C-项目】网盘(一期,线程池版)
服务器·c语言
m0_6312704044 分钟前
高级c语言(五)
c语言·开发语言
Lenyiin1 小时前
《 C++ 修炼全景指南:十 》自平衡的艺术:深入了解 AVL 树的核心原理与实现
数据结构·c++·stl
2401_858286111 小时前
53.【C语言】 字符函数和字符串函数(strcmp函数)
c语言·开发语言
程序猿练习生1 小时前
C++速通LeetCode中等第5题-无重复字符的最长字串
开发语言·c++·leetcode
2401_858120261 小时前
MATLAB中的无线通信系统部署和优化工具有哪些
开发语言·matlab
MATLAB滤波1 小时前
【PSINS】基于PSINS工具箱的EKF+UKF对比程序|三维定位|组合导航|MATLAB
开发语言·matlab
2401_858120531 小时前
MATLAB在嵌入式系统设计中的最佳实践
开发语言·matlab