从派生调用基本函数 [英] Calling base function from derived

查看:92
本文介绍了从派生调用基本函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以本教程为例 C语言中的继承和多态性,因为我已经根据我的确切要求对其进行了自定义,所以在尝试调用基本函数时会引发错误.

I took this tutorial example Inheritance and Polymorphism in C and because I've customized it for my exact requirements it's throwing an error when I try to call base function.

问题:为什么它在employee.c的第8行中失败并且可能的解决方法

((Employee *)self)->super.display(self); // Sementation fault: 11

下载项目

main.c

#include "person.h"
#include "employee.h"
#include <stdio.h>

int main() {
    Person* person = newPerson("John Doe");
    Employee* employee = newEmployee("Jane Doe", "Acme", 40000);

    person->display(person); // displaying Person object
    puts("------");
    employee->display((Person*)employee); // displaying employee info

    return 0;
}

Person.h

#ifndef _PERSON_H
#define _PERSON_H

#include <stdlib.h>

typedef struct Person Person;
struct Person {
    char* name;
    void (*display)(Person*);
};

Person* newPerson(char* name);

#endif

Person.c

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

static void display(Person* const self) {
    printf("Name: %s\n", self->name);
}

Person* newPerson(char* name) {
    Person* person = malloc(sizeof(Person));
    person->name = name;
    person->display = display;
    return person;
}

Employee.h

#include "person.h"

typedef struct Employee Employee;
struct Employee {
    Person super;
    char* company;
    int salary;
    void (*display)(Person*);
};

Employee* newEmployee(char* name, char* company, int salary);

Employee.c

#include "employee.h"
#include <string.h>
#include <stdio.h>

static void display(Person* const self) {
    puts(((Employee*)self)->super.name); // works
//    ((Employee *)self)->super.display(self); // Sementation fault: 11
    printf("Company: %s\n", ((Employee *)self)->company);
    printf("Salary: %d\n", ((Employee*)self)->salary);
}

Employee* newEmployee(char* name, char* company, int salary) {
    Employee* employee = malloc(sizeof(Employee));

    employee->super.name = name;

    employee->company = company;
    employee->salary = salary;
    employee->display = display;

    return employee;
}

推荐答案

问题是因为Employee中的嵌入式结构没有初始化显示函数指针并指向函数

The problem was because embedded struct in Employee didn't have display function pointer initialized and pointed to a function

struct Employee {
    Person super;
    ...
 }

解决方案::将嵌入式结构Person更改为指针类型,并调用newPerson以获取超级

Solution: Change the embedded structure Person to pointer type and call newPerson for super

employee.h

typedef struct Employee Employee;
struct Employee {
    Person *super; // change this pointer type
    char* company;
    int salary;
    void (*display)(Person*);
};

Employee* newEmployee(char* name, char* company, int salary);

员工.c

static void display(Person* const self) {
    ((Employee*)self)->super->display(((Employee*)self)->super);

    printf("Company: %s\n", ((Employee *)self)->company);
    printf("Salary: %d\n", ((Employee*)self)->salary);
}

Employee* newEmployee(char* name, char* company, int salary) {
    Employee* employee = malloc(sizeof(Employee));

    employee->super = newPerson(name); // call constructor here

    employee->company = company;
    employee->salary = salary;
    employee->display = display;

    return employee;
}

这篇关于从派生调用基本函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆