Main Content

Pass const and Non-const Parameters

A const parameter can take a non-const object, but a non-const parameter cannot take a const object. For example, this C++ class has functions that create const and non-const objects and methods that take const and non-const inputs.

Display Help for Interface

Suppose that you have an interface with the definition in const.hpp. To run this example, follow the instructions in Build Interface for const and Non-const Example to generate a MATLAB® interface named libconst.

When you display help for libconst, you see that it contains class MyClass and functions returnConstObj and returnNonConstObj.

help clib.libconst
Classes contained in clib.libconst:
MyClass                        -  clib.libconst.MyClass    Representation of C++ class MyClass.


Functions contained in clib.libconst:
returnConstObj                 -  clib.libconst.returnConstObj Representation of C++ function returnConstObj.

returnNonConstObj              -  clib.libconst.returnNonConstObj Representation of C++ function returnNonConstObj.

Display help for the functions in the interface. returnConstObj returns a read-only value. returnNonConstObj does not have a qualifier on its return value.

help clib.libconst.returnConstObj
returnConstObj -  clib.libconst.returnConstObj Representation of C++ function returnConstObj.

  RetVal = clib.libconst.returnConstObj
    Output Arguments
      RetVal         read-only clib.libconst.MyClass  
help clib.libconst.returnNonConstObj
returnNonConstObj -  clib.libconst.returnNonConstObj Representation of C++ function returnNonConstObj.

  RetVal = clib.libconst.returnNonConstObj
    Output Arguments
      RetVal         clib.libconst.MyClass  

Call Functions in MATLAB

Create a MyClass const object obj1.

obj1 = clib.libconst.returnConstObj;

Confirm that the object is read-only by calling clibIsReadOnly, and then call its constMethod.

clibIsReadOnly(obj1)
ans = 
   1
obj1.constMethod;

Confirm that obj1, a read-only object, is not a valid input to constMethod.

obj1.nonConstMethod;
Error using clib.libconst.MyClass/nonConstMethod
Calling nonconst method 'nonConstMethod' of read-only object 'clib.libconst.MyClass' not supported.

You can, however, pass a non-const object to both its constMethod and nonConstMethod methods.

obj2 = clib.libconst.returnNonConstObj;
clibIsReadOnly(obj2)
ans = 
   0
obj2.constMethod;
obj2.nonConstMethod;

Build Interface for const and Non-const Example

This C++ header file defines functions that take const and non-const parameters.

class MyClass {
public:
      int m_value;
      MyClass():m_value(0) {}
      void nonConstMethod() {}
      int constMethod() const { return m_value; }
} classObj;

MyClass& returnNonConstObj() {
    return classObj;
}

const MyClass& returnConstObj() {
    return classObj;
}

Save this code in a header file named const.hpp, then generate a library definition file for a MATLAB interface named libconst following the instructions in Header-Only HPP File.

Library ArtifactsMATLAB Interface libnameMATLAB Help

Header file const.hpp

clib.libconst

>> help clib.libconst