Clear Filters
Clear Filters

Using operator overloading with clib

5 views (last 30 days)
Peter Hristov
Peter Hristov on 25 Aug 2021
Answered: arushi on 7 May 2024
I am building an interface for a shared library compiled with Visual Studio C++ 2019. All of the class methods from the library (exept those having a const modifier, see here) run as expected. However, significant feature of the class is that it overloads some aritmetic and comparison operators to work on objects of that class. No support for operator overloading is a known limitation in clib. What would be the best workaround for this? Can it be manually included in the define... .mlx/.m files prior to building the .dll, or does it need to be included as a wrapper, external to the definitions?

Answers (1)

arushi
arushi on 7 May 2024
Hi Peter,
When integrating C++ libraries with MATLAB, particularly those utilizing features not directly supported by MATLAB's C interface (such as operator overloading), creating a wrapper around the C++ code is a common and effective strategy. This wrapper translates the C++ classes and their operations into functions that can be called from MATLAB, effectively exposing the C++ library's functionality in a way that MATLAB can interact with.
1. Create Wrapper Functions - For each overloaded operator in your C++ class, create a corresponding wrapper function. This function will explicitly call the operator and provide a way to invoke it from MATLAB.
2. Expose the Wrapper Functions to MATLAB - After creating wrapper functions, you need to expose them to MATLAB. This involves declaring them in a way that MATLAB's C interface can interact with, typically through a DLL (Dynamic Link Library) on Windows.You'll compile your C++ code along with the wrapper functions into a DLL.
3. Call the Wrapper Functions from MATLAB.
Hope this helps.

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!