Main Content

Pass C++ Language Opaque Objects

An opaque object has no properties and methods visible to MATLAB®. You can pass these objects to related functions that know how to work with them. Consult the documentation for the function that returned the opaque object to learn more about how to use it.

To run this example, follow the instructions in Build Interface for Opaque Object Example to generate a MATLAB interface named libopaque.

Display Help for Interface

Suppose that you have an interface to opaque.hpp. When you display help for the library, you see that the SessionHandle class is an opaque type.

help clib.libopaque
Classes contained in clib.libopaque:
SessionHandle                  -  clib.libopaque.SessionHandle    C++ opaque type.


Functions contained in clib.libopaque:
closeHandle                    -  clib.libopaque.closeHandle Representation of C++ function closeHandle.

getHandle                      -  clib.libopaque.getHandle Representation of C++ function getHandle.

Display help for the functions in the library. getHandle returns a SessionHandle, and closeHandle takes a SessionHandle as input.

help clib.libopaque.getHandle
getHandle -  clib.libopaque.getHandle Representation of C++ function getHandle.

  RetVal = clib.libopaque.getHandle
    Output Arguments
      RetVal         clib.libopaque.SessionHandle  
help clib.libopaque.closeHandle
closeHandle -  clib.libopaque.closeHandle Representation of C++ function closeHandle.

  clib.libopaque.closeHandle(SessionHandle)
    Input Arguments
      SessionHandle  clib.libopaque.SessionHandle  

Call Function in MATLAB

Create a SessionHandle object by calling getHandle.

sHandle = clib.libopaque.getHandle
sHandle =
    SessionHandle is an opaque object.

Pass the MATLAB sHandle variable to the closeHandle function.

clib.libopaque.closeHandle(sHandle)

Build Interface for Opaque Object Example

In this C++ header file, SessionHandle is an opaque type defined as typedef void*.

typedef void* SessionHandle;
SessionHandle getHandle(){
    int* a = new int(1);
    return a;
}
void closeHandle(SessionHandle h){
    int* a = (int*)(h);
    delete a;
}

Save this code in a header file named opaque.hpp, then generate a MATLAB interface named libopaque following the instructions in Header-Only HPP File.

Library ArtifactsMATLAB Interface libnameMATLAB Help

Header file opaque.hpp

clib.libopaque

>> help clib.libopaque

See Also

Topics