- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试自学面向对象 C 的基础知识。我正在尝试构建 vtable,然后使用它们来模拟结构的“继承”(尝试在 C++ 中复制类继承)。
我有一个问题..我相信这是可能的,但我不知道该怎么做。可以从“基”结构的指针修改“派生”结构的变量吗?
#include <stdio.h>
#include <stdlib.h>
//should represent Base
typedef struct Animal
{
int age;
void *vtable;
} Animal;
//Cat and dog will inherit Animal. They both have same 'age' variable, and different other parameters
typedef struct Dog
{
int age;
double weight;
void *vtable;
} Dog;
typedef struct Cat
{
int age;
int numberOfLives;
void *vtable;
} Cat;
//some test functions
void Speak_Dog(Animal* a)
{
printf("Woof!\n");
}
void Speak_Cat(Animal* a)
{
printf("Meow!\n");
}
//this is where I'm stuck, I would like to keep sending pointer to Animal
double Dog_GetCost(Animal *a)
{
return 0;//should return age*weight
}
double Cat_GetCost(Animal *a)
{
return 0; //should return age*num_lives
}
//build tables
void* Dog_Vtable[2] = {Speak_Dog, Dog_GetCost};
void* Cat_Vtable[2] = {Speak_Cat, Cat_GetCost};
void Construct_Dog(Dog* d)
{
d->age = 0;
d->weight = 0;
d->vtable = Dog_Vtable;
}
void Construct_Cat(Cat* c)
{
c->age = 0;
c->numberOfLives = 0;
c->vtable = Cat_Vtable;
}
int main()
{
int choice;
Dog d;
Cat c;
Animal* a;
((void (*)(Animal*))Dog_Vtable[0])((Animal*)&d); //print out "woof" - good
((void (*)(Animal*))Cat_Vtable[0])((Animal*)&c); //print out "meow" - good
printf("Do you want to make dog or a cat? (0/1) ");
scanf("%d", &choice);
if(choice == 0)
{
a = &d; //animal is Dog
a = (Animal*)malloc(sizeof(Dog)); //allocate memory for Dog
Construct_Dog(a); //construct it
}
else
{
a = &c; //similar for cat
a = (Animal*)malloc(sizeof(Cat));
Construct_Cat(a);
}
free(a);
return 0;
}
现在,假设我尝试修改第二个 int 变量(权重或 NumLives,具体取决于),我将如何使用 *a
更改它?我正在尝试学习 使用 ANSI-C 进行面向对象编程
,但我无法弄清楚这一点。
最佳答案
您可以使用((Dog *)a) -> Weight = 42.0;
或((Cat *)a) -> numberOfLives = 9;
关于c - 面向对象的C : Building vtables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36611591/
我正在创建一个多区域 AlwaysOn Sql Availability 组。我在东部有 2 个虚拟机,在西部有 1 个虚拟机。我需要使用面向公众的负载平衡器对这些进行负载平衡。是否可以使用存在于不同
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我是 Smalltalk 技术的新手。我的编程经验是使用 C 和 C++。我想了解smalltalk的设计方法论。谁能推荐一些用smalltalk开发的简单的实时应用程序,可以帮助像我这样的初学者探索
现在我已经对 web2py 比较熟悉了,我想试试 Django。 主要区别是什么? 考虑到 web2py 知识,最有效的入门方法是什么? (一定有一些python应用框架的知识,不是吗?) 编辑 另外
我之前发过一个问题,Moving away from VSS ,其中我向使用 VSS 的 Delphi 开发人员询问了最佳的 VCS 控件。大多数开发人员似乎将 svn 与 TortoiseSVN 一
引用this question ,收集对 Delphi 初学者真正有帮助的网站链接可能会很好。 第一个答案指向Delphi Basics ,这是一个非常好的网站。 但一定还有更多。 所以:请为这个社区
我已经构建了一个 saas 产品,其中 Angular 4 与 golang Rest api 集成,并将构建上传到了 aws ec2 实例上。我的项目是一个基于 Multi-Tenancy 的应用程
我是 .NET 开发人员,在此之前使用过 VB6。我已经非常熟悉这些环境,并在垃圾收集语言的上下文中工作。但是,我现在希望通过 native C++ 增强我的技能,但发现自己有点不知所措。具有讽刺意味
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
由于我在数学方面没有任何出色的技能,所以我问您是否存在我应该在未来可能会改变的类(class)中使用的算法。 考虑以下场景: “角色”类具有以下字段: private boolean admin; p
我有许多面向“列”的 csv 文件,我需要对其进行预处理才能最终索引它们。 这是面向时间的数据,每个“设备”都有大量的列(最多 128 列),例如: LDEV_XXXXXX.csv
我想就重构技巧的最佳资源展开讨论,着眼于前端 JavaScript 重构。 一个我尊重其意见的 friend 建议这个 book ,尽管它使用 Java 中的示例。我明白 OO 重构的原则应该翻译成另
我不明白如何使用 Jquery/javascript 处理对象。 以及如何创建私有(private)方法/变量?我在论坛上看到关闭,但我尝试过但没有用。以及如何查看方法/变量是否是私有(private
我已经使用 MATLAB 多年并且非常熟练。我习惯了 MATLAB 调试,您可以在其中跳过行并查看变量及其变化方式,这就是我学习编码的方式。我现在正在研究使用 FORTRAN、C++ 和 Python
我正在学习如何实现“单一 Activity - 多个 fragment ”架构。据我所知,这是如今很普遍的方法,你可以找到很多关于这个主题的文章。例如这个 http://vinsol.com/blog
目前是否有任何简单的方法可以在 Apache Tomcat Servlet 容器前面设置 YAWS Web 服务器,类似于流行的 Apache httpd + Tomcat 设置? 最佳答案 我不相信
我是一个相当有能力的 Ruby 脚本编写者/程序员,但一直感到要扩展到 C++ 的压力。我没能找到任何与“C++ for Ruby Programmers”相关的网站。 This site Pytho
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 5 年前。
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我在表上有一个名为 cart_product_fti 的全文索引 每当我使用它时,我都必须准确列出索引中的所有字段: MATCH (foo,bar,etc) 我的想法是,如果稍后我决定向索引添加/删除
我是一名优秀的程序员,十分优秀!