在C结构多态性最好的方法 [英] Best approach for struct polymorphism in C

查看:138
本文介绍了在C结构多态性最好的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个简单的2D矢量对象。这将有x和y分量,和长度,跨产品等的方法。关键是,我想有结构,有许多可能的类型(字符,整型,浮点,双精度等)。我想知道什么是最好的选择,设计明智的,与对象进行交互?下面是我目前考虑:

I'm writing a simple 2d vector object. It will have x and y components, and length, cross product, etc. methods. The thing is, I want to have the structure to have many possible types (char, int, float, double, etc.). I was wondering what would be the best choice, design wise, to interact with the object? Here's what I'm currently considering:

1。有用户通过矢量对象专门的功能,如:

Vector2Dt_Dot(Vec2Dt* vector1, Vec2Dt* vector2);

其中'T'是载体的类型。但是,这种方法的问题是,它从相互交融不允许不同的类型,所以我不能说计算一个浮动的Vector2D和双的Vector2D的点积。第二种方法,什么我倾向于:

where 't' is the type of the vector. However, the problem with this approach is that it disallows different types from interacting with each other, so I couldn't say calculate the dot product of a float vector2d and a double vector2d. A second approach, and what I'm leaning towards:

2。让用户通过矢量对象(S)为空指针,​​与他们一起的类型,如:

Vector2D_Dot(void* vector1, unsigned vector1_type, void* vector2, unsigned vector2_type);

显然这种方法是更紧凑的API逐并且还解决了上述问题,但在一些额外的参数和类型安全的成本。

obviously this approach is more compact API-wise and also solves the above problem, but at the cost of a few extra parameters and type safety.

有可能有其他的解决方案,我不知道,但这些都是我目前考虑的。你觉得什么是最好的方法呢?

There may be other solutions that I'm not aware of, however these are the ones I'm currently considering. What do you feel is the best approach to this?

推荐答案

你可以做的就是使用多态对象。定义的结构是这样的:

What you can do is to use polymorphic objects. Define the structures like this:

#define INT_TYPE 0
#define DOUBLE_TYPE 1
//more type constants

typedef struct Vector2D {
    int type;
} Vector2D;

typedef struct Vector2D_int {
    Vector2D super;
    int x, y;
} Vector2D_int;

typedef struct Vector2D_double {
    Vector2D super;
    double x, y;
} Vector2D_double;

//more typed vector structures

然后,你可以写你的函数接受的Vector2D 指针,检查各自的类型字段和他们丢在适当的类型化的变量来访问有效载荷数据。

Then you can write your functions to accept Vector2D pointers, inspect their respective type fields and cast them down to the appropriate typed variant to access the payload data.

double Vector2D_length(const Vector2D* vector) {
    if(vector->type == TYPE_INT) {
        const Vector2D_int* intVector = (Vector2D_int*)vector;
        return sqrt(intVector->x * intVector->x + intVector->y * intVector->y);
    }
    if(vector->type == TYPE_DOUBLE) {
        const Vector2D_double* doubleVector = (Vector2D_double*)vector;
        return sqrt(doubleVector->x * doubleVector->x + doubleVector->y * doubleVector->y);
    }
    //other cases follow
}

这是用手工多态性$ C​​ $ CD。所有你需要保证,是,是,键入字段总是被设置为正确的值(创建一个类型的载体时,设置一次)。

This is polymorphism coded by hand. All you need to ensure, is, that the type field is always set to the correct value (set once when a typed vector is created).

这个办法你的第二个想法的好处是,你不必通过围绕另一个变量的向量这将使使用矢量繁琐和容易出错的类型。

The advantage of this approach to your second idea, is that you do not have to pass around the type of the vectors in another variable which would make using your vectors tedious and error prone.

作为一种替代方法,你可以定义你的键入字段包含一个指向函数指针的结构。您可以创建每个你定义输入向量类型此函数指针结构的一个对象,并用它来查找其作用与给定的载体使用。这种做法是非常接近到什么C ++引擎盖下呢。

As an alternative, you can define your type field to contain a pointer to a structure of function pointers. You would create one object of this function pointer structure per typed Vector type you define, and use it to lookup which function to use with the given vector. This approach would be very close to what C++ does under the hood.

这篇关于在C结构多态性最好的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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