How to do multiple inheritance for Matlab C++ Mex file?

Dear all,
I would like to generate multiple Mex-Files that all have a similar structure. They all look somethings like this:
class MexFunction : public matlab::mex::Function {
void operator()(ArgumentList inputs, ArgumentList outputs);
void doSomeThing();
};
The operator function is always the same, but they all differ in the doSomeThing function. My idea was now to declare a base class like this
class MexBaseFunction : public matlab::mex::Function {
void operator()(ArgumentList inputs, ArgumentList outputs);
virtual void doSomeThing() = 0;
};
where I define the common operator() function once and then only change doSomeThing
class MexFunction : public MexBaseFunction {
void doSomeThing() override;
};
This doesn't work, because in order to define the base class, I have to include mexAdapter.hpp where a lot of stuff is going on that need a class called MexFunction.
My second attempt was to use multiple inheritance by defining a class
class MexBaseFunction {
void operator()(ArgumentList inputs, ArgumentList outputs);
virtual void doSomeThing() = 0;
};
that doesn't inherite from matlab::mex::Function. Then my mex-Functions would have the structure
class MexFunction : public matlab::mex::Function, public MexBaseFunction {
void doSomeThing() override;
};
This doesn't work as well, because I need to include various header files in my base class, because there I need ArgumentList, Struct, CellArray etc. In these header files various functions are defined. In my class that defines MexFunction I need to include the same files and, thus, get errors, because some functions are defined multiple times.
Now my question is:
Is there a proper approach to this problem - are forward-declarations a way to go?
Thanks in advance and best regards,
Torsten

3 comentarios

I have a similar problem. I assume you have tried to approach this "properly", i.e. you have a separate file MexBaseFunction.cpp and its header which you then compile at the same time and link in... e.g. mex mainSource.cpp MexBaseFunction.cpp -output myMex . This will produce all sorts of bizzare problems revolving around there not being MexFunction::operator() defined. I found that this does not happen while your base class is in the same file as the MexFunction class.
So if you have a header only class as a base class and derive from that you won't see the problems. I find this solution unelegant, but it fixes the problem.
Excellent suggestion, thanks a lot.
I am having exactly the same problem. Have you found another solution to this?

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 28 de Nov. de 2018

Comentada:

el 19 de Nov. de 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by