当前位置: 首页 > news >正文

网站中的表单怎么做怎么建网站教程图解

网站中的表单怎么做,怎么建网站教程图解,tp框架做网站的优点,gis做图网站从整体来看&#xff0c;笔者的做法与之前的模拟多态十分相似&#xff0c;毕竟c多态的实现与虚函数密切相关 废话少说&#xff0c;see my code&#xff1a; kernel.c#include "kernel.h" #include <stdio.h>void shape_draw(struct shape_t* obj) {/* Call dr…

从整体来看,笔者的做法与之前的模拟多态十分相似,毕竟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_Hstruct 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图如下:

UML

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

http://www.yidumall.com/news/86069.html

相关文章:

  • 用nas建设服务器网站百度推广管家登录
  • 网站模板怎么做下载百度手机助手
  • 代驾软件开发流程seo公司发展前景
  • 杭州建设主管部门的网站关键词优化工具有哪些
  • 海外注册域名的网站员工培训
  • 专业制作pptseo快速推广
  • 网站系统维护一个月正常吗网站的seo方案
  • 网站需要网监备案seo引擎搜索网站关键词
  • 深圳建设网站公百度网
  • 9377游戏官网seo全网优化指南
  • 做网站如何规避法律风险上海网络推广外包公司
  • 网站收录提交接口推广手段和渠道有哪些
  • dede可以做商城网站吗免费的网站推广方法
  • 班级网站模板素材百度指数人群画像哪里查询
  • 完全的图片宣传网站怎么做百度账号登录中心
  • 做公司网站都需要付什么费用怎么网上推广自己的产品
  • 企业如何免费做网站朝阳seo排名优化培训
  • 往网站上做新东西需要什么一个产品的网络营销方案
  • 哪个做砍价活动的网站好四川seo整站优化
  • 外贸电子网站百度seo多久能优化关键词
  • 新站秒收录接口seo顾问服务
  • 呼伦贝尔市建设局网站百度广告管家
  • 旧域名怎么做新网站seo全称是什么意思
  • 整个网站建设中的关键步骤是疫情最新数据
  • 网站建设推广需要多少钱怎么制作自己的网站网页
  • 管理员怎么看网站在线留言360优化大师最新版下载
  • 帮别人做网站哪里可以接单网络营销企业网站
  • 贵州安顺做公司网站怎样制作网页
  • 数码公司网站建设调查天津海外seo
  • 网站建设的优点长春网站优化流程