Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C语言中的极态射应用问题_C_Oop_Polymorphism - Fatal编程技术网

C语言中的极态射应用问题

C语言中的极态射应用问题,c,oop,polymorphism,C,Oop,Polymorphism,这是我在stackowerflow的第一个问题。所以我很兴奋:) 我正在练习C语言中的多态性。我读过很多文章。我用我所理解的编写了一个小样本代码。我的目标是创建一个shape类。此类包括字段和长度方法。从该类派生的矩形类和圆形类自行计算字段和长度方法 但在编译过程中存在一个问题: 在_C_示例_3_多态性_3.exe中的OOP_中的0x000000000000000A处引发异常:0xC000005:访问冲突执行位置0x000000000000000A 当我删除其中一行时,效果会更好。但这一次,它

这是我在stackowerflow的第一个问题。所以我很兴奋:) 我正在练习C语言中的多态性。我读过很多文章。我用我所理解的编写了一个小样本代码。我的目标是创建一个shape类。此类包括字段和长度方法。从该类派生的矩形类和圆形类自行计算字段和长度方法

但在编译过程中存在一个问题: 在_C_示例_3_多态性_3.exe中的OOP_中的0x000000000000000A处引发异常:0xC000005:访问冲突执行位置0x000000000000000A

当我删除其中一行时,效果会更好。但这一次,它是根据上次调用的构造函数进行操作的:

我哪里有问题

形状h:

#include <stdint.h>

struct Vtable;

typedef struct{
    struct Vtable* vptr;
    uint16_t x;
    uint16_t y;

}Shape;

struct Vtable {
    void(*area)(Shape* me);
    void(*lenght)(Shape* me);
};

inline void area(Shape* me) {
    (*me->vptr->area)(me);
}

inline void lenght(Shape* me) {
    (*me->vptr->lenght)(me);
}

void shape_ctor(Shape* me, int x, int y);
矩形h:

#include "shape.h"

typedef struct  {
    Shape super;
    uint16_t en;
    uint16_t boy;
}rect;

void rect_ctor(rect* me, uint16_t x, uint16_t y, uint16_t en, uint16_t boy);
c节:

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

void rect_lenght(void* me);
void rect_area(void* me);


void rect_ctor(rect* me,uint16_t x, uint16_t y, uint16_t en, uint16_t boy) {
    struct Vtable vtbl = { &rect_area, &rect_lenght };
    
    shape_ctor(&me->super, x, y);
    me->super.vptr = &vtbl;                 //override vptr
    me->en  = en;
    me->boy = boy;
}


void rect_lenght(void* me) {
    rect* me_ = (rect*)me;      //downcast işlemi
    printf("rectangle lenght: %d\n",(me_->en + me_->boy) * 2);
}
void rect_area(void* me) {
    rect* me_ = (rect*)me;      //downcast işlemi
    printf("rectangle area: %d\n", (me_->en)*(me_->boy));
}
圆圈c:

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

void circle_lenght(void* me);
void circle_area(void* me );


void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r) {
    struct Vtable vtbl = {&circle_area,&circle_lenght};

    shape_ctor(&me->super, x, y);
    me->super.vptr = &vtbl;
    me->radius = r;
}


void circle_lenght(void* me) {
    circle* me_ = (circle*)me;
    printf("circle lenght: %f\n", (me_->radius) * (2) * (3.14));
}
void circle_area(void* me){
    circle* me_ = (circle*)me;
    printf("circle area: %f\n", (me_->radius) * (me_->radius) * (3.14));
}
#包括“circle.h”
#包括
无效圆圈长度(无效*我);
空心圆面积(空心*me);
无效圆(圆*me、uint16\t x、uint16\t y、uint16\t r){
结构Vtable vtbl={&circle\u area,&circle\u length};
形状系数(&me->super,x,y);
me->super.vptr=&vtbl;
me->radius=r;
}
无效圆长度(无效*我){
圆圈*我=(圆圈*)我;
printf(“圆的长度:%f\n”,(me_u2;->半径)*(2)*(3.14));
}
空心圆面积(空心*me){
圆圈*我=(圆圈*)我;
printf(“圆面积:%f\n”,(me_uu2;->半径)*(me_2;->半径)*(3.14));
}
主要条款c:

#include <stdio.h>
#include "rect.h"
#include "circle.h"


int main() {
    rect  k1;
    circle d1;

    rect_ctor(&k1, 10, 10, 15, 20);
    circle_ctor(&d1, 10, 10, 5);
    
    
    k1.super.vptr->lenght(&k1);
    d1.super.vptr->lenght(&d1);
}
#包括
#包括“rect.h”
#包括“circle.h”
int main(){
rect-k1;
圈d1;
纠正器(&k1、10、10、15、20);
圆圈(d1、10、10、5);
k1.super.vptr->lenght(&k1);
d1.超级vptr->长度(&d1);
}
但是在编译过程中有一个问题:在这里输入图像描述

这是在执行期间,而不是在编译期间

您有未定义的行为,因为您在离开函数后使用了局部变量的地址,需要替换

void shape_ctor(Shape* me,int x, int y) {   
    struct Vtable vtbl = { &area,&lenght };

我不得不将面积和长度的定义从shape.h移到shape.c,并删除了它们的无用内联

我还必须保护头部不受多个内含物的影响,例如:

#ifndef SHAPE_H
#define SHAPE_H

#include <stdint.h>

struct Vtable;

typedef struct{
    struct Vtable* vptr;
    uint16_t x;
    uint16_t y;
}Shape;

struct Vtable {
    void(*area)(Shape* me);
    void(*lenght)(Shape* me);
};

void shape_ctor(Shape* me, int x, int y);

#endif
修改main以分配堆中的元素,以允许valgrind知道它们的大小,从而检测可能的访问:

#include <stdlib.h>

#include "rect.h"
#include "circle.h"

int main() {
    rect  * k1 = malloc(sizeof(*k1));
    circle * d1 = malloc(sizeof(*d1));

    rect_ctor(k1, 10, 10, 15, 20);
    circle_ctor(d1, 10, 10, 5);
    
    
    k1->super.vptr->lenght(k1);
    d1->super.vptr->lenght(d1);
    
    free(k1);
    free(d1);
    
    return 0;
}

为了在编译时不出现所有这些警告,我鼓励您添加所需的强制转换


注意:定义vtbl也更好,您不希望它们被错误破坏,所以

void shape_ctor(Shape* me,int x, int y) {   
    static const struct Vtable vtbl = { &area,&lenght };

void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r) {
    static const struct Vtable vtbl = {&circle_area,&circle_lenght};
和形状。h:


声明成员/变量/参数
const
越多,在编译时检测错误就越好。

请将错误添加为文本而不是图像。错误看起来像是运行时异常,而不是编译错误。
但编译过程中出现了一个问题:在此处输入图像描述
:似乎his是在执行过程中,而不是在编译过程中,无论如何,我无法理解您如何编译代码而不出错访问冲突意味着您正在写入内存中您不拥有的位置。查找为写入内容创建的缓冲区太小的缓冲区,或未分配内存的指针。而且,我同意第一个评论者,请考虑编辑你的代码,把文章中的错误信息包含在文本中,而不是隐藏在图片的链接中。顺便说一句,
lenght
拼写为
length
:)否则,这个问题做得很好。(+1)使用“const”和“static”关键字是解决方案。非常感谢。但您不建议使用内联函数吗?它们在我看到的所有示例中都使用内联。@Cemal它们根本不使用内联,used inline意味着调用是/可以被主体替换,但您通过指向它们的指针使用它们,所以您需要它们“真的”defined@Cemal欢迎你,我回答你的问题,因为问题和使用的实现非常有趣,这让我想起很久以前,我在其中实现了小型C解释器,例如'eval'是多态的
void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r) {
    struct Vtable vtbl = {&circle_area,&circle_lenght};
void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r) {
    static struct Vtable vtbl = {&circle_area,&circle_lenght};
#ifndef SHAPE_H
#define SHAPE_H

#include <stdint.h>

struct Vtable;

typedef struct{
    struct Vtable* vptr;
    uint16_t x;
    uint16_t y;
}Shape;

struct Vtable {
    void(*area)(Shape* me);
    void(*lenght)(Shape* me);
};

void shape_ctor(Shape* me, int x, int y);

#endif
/tmp % gcc -Wall -g *.c
circle.c: In function 'circle_ctor':
circle.c:9:19: warning: initialization from incompatible pointer type [enabled by default]
     static struct Vtable vtbl = {&circle_area,&circle_lenght};
                   ^
circle.c:9:19: warning: (near initialization for 'vtbl.area') [enabled by default]
circle.c:9:19: warning: initialization from incompatible pointer type [enabled by default]
circle.c:9:19: warning: (near initialization for 'vtbl.lenght') [enabled by default]
main.c: In function 'main':
main.c:14:5: warning: passing argument 1 of 'k1.super.vptr->lenght' from incompatible pointer type [enabled by default]
     k1.super.vptr->lenght(&k1);
     ^
main.c:14:5: note: expected 'struct Shape *' but argument is of type 'struct rect *'
main.c:15:5: warning: passing argument 1 of 'd1.super.vptr->lenght' from incompatible pointer type [enabled by default]
     d1.super.vptr->lenght(&d1);
     ^
main.c:15:5: note: expected 'struct Shape *' but argument is of type 'struct circle *'
rect.c: In function 'rect_ctor':
rect.c:9:19: warning: initialization from incompatible pointer type [enabled by default]
     static struct Vtable vtbl = { &rect_area, &rect_lenght };
                   ^
rect.c:9:19: warning: (near initialization for 'vtbl.area') [enabled by default]
rect.c:9:19: warning: initialization from incompatible pointer type [enabled by default]
rect.c:9:19: warning: (near initialization for 'vtbl.lenght') [enabled by default]
/tmp % ./a.out
rectangle lenght: 70
circle lenght: 31.400000
/tmp % 
#include <stdlib.h>

#include "rect.h"
#include "circle.h"

int main() {
    rect  * k1 = malloc(sizeof(*k1));
    circle * d1 = malloc(sizeof(*d1));

    rect_ctor(k1, 10, 10, 15, 20);
    circle_ctor(d1, 10, 10, 5);
    
    
    k1->super.vptr->lenght(k1);
    d1->super.vptr->lenght(d1);
    
    free(k1);
    free(d1);
    
    return 0;
}
/tmp % valgrind ./a.out
==53229== Memcheck, a memory error detector
==53229== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==53229== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==53229== Command: ./a.out
==53229== 
rectangle lenght: 70
circle lenght: 31.400000
==53229== 
==53229== HEAP SUMMARY:
==53229==     in use at exit: 0 bytes in 0 blocks
==53229==   total heap usage: 2 allocs, 2 frees, 48 bytes allocated
==53229== 
==53229== All heap blocks were freed -- no leaks are possible
==53229== 
==53229== For counts of detected and suppressed errors, rerun with: -v
==53229== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
/tmp % 
void shape_ctor(Shape* me,int x, int y) {   
    static const struct Vtable vtbl = { &area,&lenght };

void circle_ctor(circle* me, uint16_t x, uint16_t y, uint16_t r) {
    static const struct Vtable vtbl = {&circle_area,&circle_lenght};
typedef struct{
    const struct Vtable* vptr;