Main Content

Use MATLAB Handle Classes in C++

Overview

The MATLAB® engine API for C++ supports MATLAB handle classes when using the strongly typed interface. This feature translates the behavior of MATLAB classes that inherit from handle classes into equivalent C++ code, preserving specific features and behaviors. (since R2024a)

Key Features

When you generate a C++ header file from a MATLAB class that inherits from a handle class using the matlab.engine.typedinterface.generateCPP function, you get this functionality:

  • Copy behavior: The generated C++ code replicates MATLAB handle class copy behavior. In MATLAB, handle objects are reference types, meaning that when you copy these objects, both the original and the new variable refer to the same object.

  • Comparison operators: The C++ representation of MATLAB handle classes supports comparison operations. You can compare C++ objects, derived from MATLAB handle classes, using the standard operators ==, !=, <, >, <=, and >=.

  • isvalid function support: The C++ interface supports the isvalid function, which checks if handle objects are valid or have been deleted.

It is important to note that in the context of the MATLAB engine API for C++, the only functionalities currently supported are copy behavior, comparison operators, and the isvalid function.

Inherent Capabilities of MATLAB Handle Classes

Deriving from the MATLAB handle class enables a subclass to:

  • Inherit methods.

  • Define events and listeners.

  • Define dynamic properties.

  • Implement set and get methods.

  • Customize copy behavior.

Example Files

These example files demonstrate the use and integration of MATLAB handle classes with C++ applications using the engine API for C++:

  • BankAccount.m: This MATLAB class file inherits from the handle class and provides basic banking functionality. For more information, see MATLAB BankAccount Class.

  • generateHeader.m: A MATLAB script used to generate the corresponding C++ header file from the BankAccount class, illustrating the integration with the engine API for C++.

    matlab.engine.typedinterface.generateCPP( ...
        "BankAccount.hpp", ...
        Classes="BankAccount")
  • run_BankAccount.m: An example using the BankAccount class.

     run_BankAccount.m

  • BankConsoleApplication.cpp: This C++ console application demonstrates the use of the generated header file from the MATLAB BankAccount class. It replicates the functionality of the MATLAB script run_BankAccount.m, but within a C++ environment. This application shows key operations such as account creation, deposits, withdrawals, and balance inquiries, mirroring the actions performed in the MATLAB script.

     BankConsoleApplication.cpp

MATLAB BankAccount Class

 BankAccount.m

Class definition: The MATLAB class BankAccount is defined as a subclass of the handle class, which allows it to exhibit reference behavior. This means instances of this class can be passed by reference.

Private properties: The class has one private property, Balance, which is a double.

Methods: The class includes methods for depositing, withdrawing, and checking the balance. These methods ensure controlled access and modification of the Balance property.

Generated C++ BankAccount Header

 BankAccount.hpp

Class inheritance: The C++ BankAccount class does not explicitly inherit from a standard C++ class that mimics a MATLAB handle class. However, it inherits from MATLABHandleObject. This custom class is intended to provide similar reference-type behavior as a MATLAB handle class.

Constructors and overloaded operators: The MATLABHandleObject C++ class includes constructors and overloaded operators (==, !=, <, >, <=, and >=). These operators provide similar functionalities to the MATLAB handle class, which supports comparison operations.

bool operator==(const BankAccount& A, const BankAccount& B);
bool operator!=(const BankAccount& A, const BankAccount& B);
bool operator<(const BankAccount& A, const BankAccount& B);
bool operator>(const BankAccount& A, const BankAccount& B);
bool operator<=(const BankAccount& A, const BankAccount& B);
bool operator>=(const BankAccount& A, const BankAccount& B);

Method mapping: The C++ methods (deposit, withdraw, and checkBalance) correspond to the MATLAB class methods. These methods interact with MATLAB using feval, which evaluates MATLAB functions from C++.

MATLAB Signature

C++ Signature

function deposit(obj, amount)
void deposit(double amount) { 
function withdraw(obj, amount)
void withdraw(double amount) { 
function bal = checkBalance(obj)
template<>
void BankAccount::checkBalance<0>() {

Inheritance from MATLAB Handle Class

In MATLAB, inheriting from the handle class allows objects to be passed by reference. It also comes with built-in comparison operators.

In the C++ representation, the concept of a handle is replicated through the use of MATLABHandleObject and std::shared_ptr. The comparison operators are explicitly defined to mimic the behavior of a MATLAB handle class in C++.

classdef BankAccount < handle
class BankAccount : public MATLABHandleObject<MATLABControllerType>