Main Content

Call C++ Class Methods Directly from a C Function Block

This example shows how to use the C Function block to call C++ class methods. In this example, a C++ class and its methods are defined in adder_cpp.h and implemented in adder_cpp.cpp.

To run this example model, you must configure MATLAB® to use a C++ compiler. This can be done using the mex -setup C++ command, and selecting a C++ compiler.

To enable access to the C++ class from the C Function block, in the model, specify the header file and the source file in the Configuration Parameters > Simulation Target pane. In this model, the custom code header file adder_cpp.h declares an adder class along with the class methods.

In the Output Code section, the class method is called. In this example, the ports of the C Function block are configured as follows:

  • The adder class object adderObj is a persistent symbol of the C Function block, which instantiates the class object for use in the block. The object is destroyed at the end of simulation.

  • The input of the C Function block is mapped to increment, which is an input argument to the class method add_one.

  • The output of the C Function block is mapped to output, which receives the return value of the class method add_one.

See Also

Related Topics