When you generate C++ libraries or executables, the default behavior of the code generator is to produce C++ classes for the classes in your MATLAB® code. These include all MATLAB classes such as value classes, handle classes, and system objects.
If you generate C++ MEX code, the code generator produces structures for the classes in your MATLAB code. For C++ libraries and executables, you can change the default behavior of the code generator and produce structures for MATLAB classes. To make this choice, do one of the following:
In a configuration object for standalone code generation (coder.CodeConfig
or coder.EmbeddedCodeConfig
), set
TargetLang
to 'C++'
and
CppPreserveClasses
to false
.
In the MATLAB Coder™ app, in the Generate step, set Language to C++. In the project build settings, on the Code Appearance tab, clear the Generate C++ classes from MATLAB classes check box.
The following examples illustrate certain rules that the code generator follows when mapping MATLAB classes to C++ classes.
Define a MATLAB handle class
MyClass
:
classdef MyClass < handle properties publicProp = 1; end properties(Access = private) privateProp end methods function obj = MyClass(value) obj.privateProp = value; end function publicMethod(obj,value) obj.privateMethod(value); end function res = calculateSomeValue(obj) res = obj.publicProp*obj.privateProp; end end methods (Access = private) function privateMethod(obj,value) obj.publicProp = obj.publicProp + value; obj.privateProp = obj.privateProp + obj.doubleThisValue(value); end end methods(Static) function res = doubleThisValue(val) res = 2 * val; end end end
Define a MATLAB function foo
that calls uses
MyClass
:
function out = foo(x,y) obj = MyClass(x); obj.publicMethod(y); out = obj.calculateSomeValue; end
Generate a static C++ library for foo
. Specify the input argument to
be a double scalar.
codegen -config:lib -lang:c++ foo -args {0,0} -report
Code generation successful: View report
Open the code generation report and inspect the generated code. The file
foo_types.h
contains the definition of the generated C++ class
MyClass
:
class MyClass { public: MyClass *init(double value); void publicMethod(double value); static double doubleThisValue(double val); double calculateSomeValue() const; double publicProp; private: double privateProp; };
This is the code generated for the function
foo
:
double foo(double x, double y) { MyClass obj; obj.init(x); obj.publicMethod(y); return obj.calculateSomeValue(); }
This table illustrates some of the rules the code generator follows when generating C++
classes and corresponding snippets from the code generated for
MyClass
.
Rule | Code Snippet |
---|---|
The class constructor in MATLAB is mapped onto an | The file MyClass *MyClass::init(double value) { MyClass *obj; obj = this; obj->publicProp = 1.0; obj->privateProp = value; return obj; } |
In most cases, if a class member is set as private in MATLAB, it is also set as private in the generated C++ code. In certain cases, a private property in MATLAB might be changed to public in the generated code. For example,
suppose that a public method To limit the occurrence of
this phenomenon, the code generator does not inline a public method unless the
method contains the | The definition of the generated C++ class class MyClass { public: MyClass *init(double value); void publicMethod(double value); static double doubleThisValue(double val); double calculateSomeValue() const; double publicProp; private: double privateProp; }; The visibility of all data and member functions is preserved between MATLAB and the generated code. The private method
void MyClass::publicMethod(double value) { this->publicProp += value; this->privateProp += MyClass::doubleThisValue((value)); } |
Static methods in MATLAB are mapped onto static C++ methods. | The generated code for the static method
static double doubleThisValue(double val); |
Methods that do not mutate the object are marked with the
| The public method double calculateSomeValue() const; |
These are some additional usage notes and limitations for generating C++ classes from MATLAB classes:
If the name of your MATLAB
Coder project is foo.prj
, the class prototypes are contained
in the file foo_types.h
. The implementations of the methods of a
class MyClass
are contained in the file
MyClass.cpp
.
In the generated code, class hierarchies are flattened. For example, suppose that in
your MATLAB code, class B
inherits from class A
.
In the generated C++ code, classes B
and A
have
no inheritance relationship between them. In the
generated code, all properties and methods of class
A
are reproduced in the definition of class
B
.
When a MATLAB class uses different types for its properties, the code generator produces a separate C++ class for each type usage.
If a MATLAB class member has different GetAccess
and
SetAccess
attributes, the corresponding member of the generated
class has the more permissive of the two attributes. For example, if a property
prop
has the attributes (GetAccess = public, SetAccess =
private)
, prop
is defined to be public in the generated
code.
While attempting to generate standalone code that contains C++ classes for MATLAB classes, you might get a warning message if both of these conditions are true:
You choose to generate reentrant code by enabling the
MultiInstanceCode
parameter in a code configuration object,
or by enabling the Generate re-entrant code parameter
in the MATLAB
Coder app.
The destructor of a class in your MATLAB code has a persistent variable or calls another function that declares and uses a persistent variable.
In such situations, to generate code that contains C++ classes for
MATLAB classes, disable the MultiInstanceCode
or the
Generate re-entrant code parameter.
coder.CodeConfig
| coder.EmbeddedCodeConfig