R
R
rhaport2016-06-24 21:40:29
C++ / C#
rhaport, 2016-06-24 21:40:29

What is the best way to program inheritance in C?

Good day.
there is a struct Base structure and a bunch of functions to it. In addition, there are definitely several types for callback functions that take a pointer to a structure as a parameter.
There was a need to extend the structure for certain functionality. But I don’t want to change the structure itself, and I don’t need to, since this functionality is not needed in other places. What to do? There are two ideas:
1) add a field pointer to void in the struct Base structure, where thirsty people can add everything extra
2) Create a type structure

struct MyCustomStruct
{
    struct Base base;
    t_whatever whatever;
    .... 
}

Соблюдая выравнивание, указатель на struct MyCustomStruct будет также указывать на Base. Таким образом его можно будет передавать в коллбэк функции и вроде как счастье.
Покритикуйте, пожалуйста, оба подхода и если есть другая идея, поделитесь ими :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
VekaVeka, 2016-06-24
@rhaport

Это решено уже лет 30 назад.
Посмотрите WinAPI.

Сергей Протько, 2016-06-24
@Fesor

вместо наследования имеет смысл применить макросы, ибо "наследование типов" для вашей задачи насколько я понял не нужно.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question