Use C++ Class in C MEX File

The MEX file mexcpp.cpp shows how to use C++ code with a C language MEX file. The example uses functions from the C Matrix API. It uses member functions, constructors, destructors, and the iostream include file. For MEX file examples using the MATLAB® Data API, see C++ MEX Applications.

The function defines a class myData with member functions display and set_data, and variables v1 and v2. It constructs an object d of class myData and displays the initialized values of v1 and v2. It then sets v1 and v2 to your input and displays the new values. Finally, the delete operator cleans up the object.

To build this example, at the command prompt type:

mex mexcpp.cpp

The calling syntax is mexcpp(num1, num2).

Related Topics