Generate C++ Code for MATLAB Classes
When you generate C++ code, the code generator produces C++ classes for the value classes, handle classes and system objects in your MATLAB® code by default. When the code generator creates C++ classes from MATLAB classes, it:
Maps MATLAB class constructor onto C++
initmethod.Maps static MATLAB methods onto static C++ methods.
Marks methods that do not mutate the object with the
constqualifier in the generated C++ code.Maintains data encapsulation by defining private and protected MATLAB class members as private and protected in the generated C++ code. Data encapsulation in the generated C++ code can be affected by inlining controls.
Defines the class prototype for
in the header fileMyClassNameand implements the methods of the class in the fileMyClassName.h.MyClassName.cpp
If you do not want to generate C++ classes for your MATLAB classes, you can instruct the code generator to generate C-style structures for MATLAB classes by using the parameter Generate C++ classes from MATLAB classes.
Comparison Between C++ Classes and C-Style Structures
This table shows how the generated C++ code can differ based on whether you generate C++ classes or C-style structures from MATLAB classes.
| MATLAB Code | Generate C++ Classes from MATLAB classes | Generate C-Style Structures from MATLAB classes |
|---|---|---|
classdef MySimpleClass properties prop end methods function obj = MySimpleClass(x) obj.prop = x; end end end |
|
|
function sum = useMySimpleClass(x) obj1 = MySimpleClass(x); obj2 = MySimpleClass(x^2); sum = obj1.prop + obj2.prop; end |
|
|
To instruct the code generator to produce C++ code that maps MATLAB classes to C-style structures instead of C++ classes, use one of these approaches:
In a code configuration object, set the
CppPreserveClassesproperty tofalse.In the Code Generation Settings dialog box, clear the Generate C++ classes from MATLAB classes check box.
Interaction Between C++ Class Mapping and Inlining Controls
In most cases, the code generator defines private MATLAB class members as private in the generated C++ code. However, inlining can
cause the code generator to expose a private property of your MATLAB class. For example, suppose that the public method
publicMethod used the private property privateProp
of the MyExampleClass object. If the code generator inlines
publicMethod in the generated code, then the property
privateProp must be visible from outside the object and therefore the
code generator must redefine this property as a public property.
To limit the exposure of private properties via inlining, the code generator does not
inline public method calls that appear outside the class definition when you prevent
inlining between user-written MATLAB function. You can control inlining between user-written functions by using the
code configuration parameter Inline strategy for user
written MATLAB functions or by using other inlining controls. See Control Inlining to Fine-Tune Performance and Readability of Generated Code. Note
that, in general, inlining settings override the class member qualifiers
public, private, and
protected.
Additional Usage Notes and Limitations
These additional usage notes and limitations apply when generating C++ classes from MATLAB classes:
In the generated code, class hierarchies are flattened. For example, suppose that in your MATLAB code, class
MySubClassinherits from classMySuperClass. In the generated C++ code, classesMySubClassandMySuperClassdo not have an inheritance relationship. The code generator reproduces the properties and methods ofMySuperClassin the definition ofMySubClass.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
GetAccessandSetAccessattributes, the corresponding member of the generated class has the more permissive of the two attributes. For example, if a propertyprophas the attributes(GetAccess = public, SetAccess = private),propis defined to be a public property in the generated code.When you generate standalone code that contains C++ classes, the code generator can produce a warning message if you generate re-entrant, multi-instance code and the destructor method of a class in your MATLAB code has a persistent variable or calls another function that declares and uses a persistent variable. To resolve this warning, use one of these approaches:
Disable re-entrant code generation by setting the
MultiInstanceCodeproperty in a code configuration object tofalseor by clearing the Generate re-entrant code check box.Avoid using persistent variables in a class destructor or in a function called by a class destructor.
Example: Generate C++ Code From MATLAB Function that Uses Classes
This example demonstrates how the code generator maps a MATLAB handle class that has public, protected, and private members to a C++ class.
Examine MATLAB Class and Function
Examine the handle class MyExampleClass, which has public, protected, and private properties. This class also has public, private, and static methods.
type MyExampleClass.mclassdef MyExampleClass < handle
properties
publicProp = 1;
end
properties(Access = private)
privateProp
end
properties(Access = protected)
protectedCount = 0;
end
methods
function obj = MyExampleClass(value)
obj.privateProp = value;
end
function publicMethod(obj,value)
obj.privateMethod(value);
obj.protectedCount = obj.protectedCount + 1;
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
The function useMyExampleClass uses this class.
type useMyExampleClass.mfunction out = useMyExampleClass(x,y) obj = MyExampleClass(x); obj.publicMethod(y); out = obj.calculateSomeValue; end
Generate C++ Code
To generate C++ code for useMyExampleClass, first create a code configuration object for a static library and set cfg.TargetLang to "C++". Although the code generator produces C++ classes from MATLAB classes by default, optimizations later in the code generation process can result in the inlining of these C++ classes. For this example, you can prevent the code generator from inlining the class definitions by setting cfg.InlineBetweenUserFunctions to "Readability". See Inline strategy for user written MATLAB functions.
cfg = coder.config("lib"); cfg.TargetLang = "C++"; cfg.InlineBetweenUserFunctions = "Readability";
Generate a static C++ library by using the codegen command and specify the code configuration object by using the -config option. Use the -args option to specify that the input arguments are scalar doubles.
codegen -config cfg useMyExampleClass -args {0,0}
Code generation successful.
Examine Generated C++ Function
The file useMyExampleClass.cpp contains the generated C++ function.
file = fullfile("codegen","lib","useMyExampleClass","useMyExampleClass.cpp"); coder.example.extractLines(file,"double useMyExampleClass","}",1,1)
double useMyExampleClass(double x, double y)
{
MyExampleClass obj;
obj.init(x);
obj.publicMethod(y);
return obj.calculateSomeValue();
}
When the C++ function creates an instance of MyExampleClass, the generated code explicitly calls the init method. The file MyExampleClass.cpp contains the definition of the init method.
file = fullfile("codegen","lib","useMyExampleClass","MyExampleClass.cpp"); coder.example.extractLines(file,"void MyExampleClass::init","}",1,1)
void MyExampleClass::init(double b_value)
{
publicProp = 1.0;
protectedCount = 0.0;
privateProp = b_value;
}
Examine Generated C++ Class
The file MyExampleClass.h contains the definition of the generated C++ class.
file = fullfile("codegen","lib","useMyExampleClass","MyExampleClass.h"); coder.example.extractLines(file,"class MyExampleClass","};",1,1)
class MyExampleClass {
public:
void init(double b_value);
void publicMethod(double b_value);
static double doubleThisValue(double val);
double calculateSomeValue() const;
double publicProp;
protected:
double protectedCount;
private:
double privateProp;
};
The MATLAB class MyExampleClass defines the method doubleThisValue as static. The code generator also defines this method as static in the generated C++ code. The method calculateSomeValue does not mutate the object. Therefore, the code generator marks this method with the const qualifier in the generated code. The code generator does not expose the private MATLAB method privateMethod in the generated C++ class. Instead, the code generator inlines privateMethod in the definition of the public method publicMethod. The file MyExampleClass.cpp contains the definition of the publicMethod method.
file = fullfile("codegen","lib","useMyExampleClass","MyExampleClass.cpp"); coder.example.extractLines(file,"void MyExampleClass::publicMethod","}",1,1)
void MyExampleClass::publicMethod(double b_value)
{
publicProp += b_value;
privateProp += MyExampleClass::doubleThisValue(b_value);
protectedCount++;
}