You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C语言中实现基于指针的私有基类多级继承及多态?

C语言实现指针式私有多级继承与多态的解决方案

核心思路

  1. 不透明类型封装私有成员:将结构体具体定义放在实现文件中,头文件仅声明类型,外部无法直接访问基类指针或私有成员,保证封装性。
  2. 虚函数表实现多态:所有类的结构体首成员为虚表指针,虚表中存放对应类的方法实现,统一多态调用入口。
  3. 链式转换处理多级继承:为每个派生类实现向上转换函数,通过链式调用父类的转换逻辑,自动逐层获取顶层基类指针。

代码实现

头文件(shape.h)

#ifndef SHAPE_H
#define SHAPE_H

// 不透明类型声明,外部无法访问结构体内部成员
typedef struct Shape_s Shape;
typedef struct Circle_s Circle;
typedef struct ColoredCircle_s ColoredCircle;

// 多态绘制函数入口
void shapeDraw(void* obj);

// 对象创建函数
Shape* shapeCreate(int s);
Circle* circleCreate(int s, int c);
ColoredCircle* coloredCircleCreate(int s, int c, int cc);

// 对象销毁函数
void shapeDestroy(Shape* shape);
void circleDestroy(Circle* circle);
void coloredCircleDestroy(ColoredCircle* cc);

// 安全向上转换为顶层Shape的函数
Shape* circleToShape(Circle* circle);
Shape* coloredCircleToShape(ColoredCircle* cc);

#endif

实现文件(shape.c)

#include "shape.h"
#include <stdlib.h>
#include <stdio.h>

// 虚函数表结构体,定义多态方法规范
typedef struct ShapeVtable_s {
    void (*draw)(void* obj);
    Shape* (*toShape)(void* obj);
} ShapeVtable;

// 顶层基类定义
struct Shape_s {
    const ShapeVtable* vtable;
    int s; // 私有成员
};

// Shape类的绘制实现
static void shapeDrawImpl(void* obj) {
    Shape* shape = (Shape*)obj;
    printf("Shape: s = %d\n", shape->s);
}

// Shape类的自我转换实现
static Shape* shapeToShape(void* obj) {
    return (Shape*)obj;
}

// Shape类的虚表
static const ShapeVtable shapeVtable = {
    .draw = shapeDrawImpl,
    .toShape = shapeToShape
};

// Circle类定义
struct Circle_s {
    const ShapeVtable* vtable;
    Shape* base; // 私有基类指针
    int c;
};

// Circle类的绘制实现
static void circleDrawImpl(void* obj) {
    Circle* circle = (Circle*)obj;
    Shape* shape = circle->vtable->toShape(circle);
    printf("Circle: s = %d, c = %d\n", shape->s, circle->c);
}

// Circle类向上转换为Shape的实现
static Shape* circleToShapeImpl(void* obj) {
    Circle* circle = (Circle*)obj;
    return circle->base;
}

// Circle类的虚表
static const ShapeVtable circleVtable = {
    .draw = circleDrawImpl,
    .toShape = circleToShapeImpl
};

// ColoredCircle类定义
struct ColoredCircle_s {
    const ShapeVtable* vtable;
    Circle* base; // 私有基类指针
    int cc;
};

// ColoredCircle类的绘制实现
static void coloredCircleDrawImpl(void* obj) {
    ColoredCircle* cc = (ColoredCircle*)obj;
    Shape* shape = cc->vtable->toShape(cc);
    printf("ColoredCircle: s = %d, c = %d, cc = %d\n", shape->s, cc->base->c, cc->cc);
}

// ColoredCircle类向上转换为Shape的实现
static Shape* coloredCircleToShapeImpl(void* obj) {
    ColoredCircle* cc = (ColoredCircle*)obj;
    return cc->base->vtable->toShape(cc->base);
}

// ColoredCircle类的虚表
static const ShapeVtable coloredCircleVtable = {
    .draw = coloredCircleDrawImpl,
    .toShape = coloredCircleToShapeImpl
};

// 多态绘制统一入口
void shapeDraw(void* obj) {
    if (!obj) return;
    const ShapeVtable* vtable = *(const ShapeVtable**)obj;
    vtable->draw(obj);
}

// 创建函数实现
Shape* shapeCreate(int s) {
    Shape* shape = malloc(sizeof(Shape));
    shape->vtable = &shapeVtable;
    shape->s = s;
    return shape;
}

Circle* circleCreate(int s, int c) {
    Circle* circle = malloc(sizeof(Circle));
    circle->vtable = &circleVtable;
    circle->base = shapeCreate(s);
    circle->c = c;
    return circle;
}

ColoredCircle* coloredCircleCreate(int s, int c, int cc) {
    ColoredCircle* cc_obj = malloc(sizeof(ColoredCircle));
    cc_obj->vtable = &coloredCircleVtable;
    cc_obj->base = circleCreate(s, c);
    cc_obj->cc = cc;
    return cc_obj;
}

// 销毁函数实现
void shapeDestroy(Shape* shape) {
    free(shape);
}

void circleDestroy(Circle* circle) {
    shapeDestroy(circle->base);
    free(circle);
}

void coloredCircleDestroy(ColoredCircle* cc) {
    circleDestroy(cc->base);
    free(cc);
}

// 外部调用的转换函数
Shape* circleToShape(Circle* circle) {
    return circle->vtable->toShape(circle);
}

Shape* coloredCircleToShape(ColoredCircle* cc) {
    return cc->vtable->toShape(cc);
}

测试代码(main.c)

#include "shape.h"

int main() {
    Shape* shape = shapeCreate(10);
    Circle* circle = circleCreate(20, 30);
    ColoredCircle* cc = coloredCircleCreate(40, 50, 60);

    // 多态调用绘制函数
    shapeDraw(shape);
    shapeDraw(circle);
    shapeDraw(cc);

    // 安全转换为顶层Shape并调用
    Shape* circle_shape = circleToShape(circle);
    Shape* cc_shape = coloredCircleToShape(cc);
    shapeDraw(circle_shape);
    shapeDraw(cc_shape);

    // 销毁资源
    shapeDestroy(shape);
    circleDestroy(circle);
    coloredCircleDestroy(cc);

    return 0;
}

关键说明

  • 私有性保障:结构体内部定义仅在实现文件可见,外部无法直接访问base指针或私有成员,只能通过提供的函数操作。
  • 多级继承适配:每个类的转换函数通过链式调用父类逻辑,自动处理多级继承的向上转换,无需手动逐层转换。
  • 多态逻辑:所有类共享统一的shapeDraw入口,通过虚表指针调用对应类的实现,完全符合基类函数处理派生类的需求。

内容的提问来源于stack exchange,提问作者Korsarq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 06:00:21