5.5 Creating C++ MEX-files

/*  The following example illustrates how to use C++ code with your 
    C language MEX-file for MATLAB v4.x.  It makes use of member
    functions, constructors, destructors, and the iostream.
    The file is called example.cxx.  The routine simply defines
    a class, constructs a simple object, and displays the initial 
    values of the internal variables.  It then sets the data members
    of the object based on the input given to the MEX-file and displays
    the changed values.
    The calling syntax is:    example( num1, num2 )
    The mexFunction definition, math.h, and mex.h are wrapped with
    extern "C" so that it has "C linkage" for the benefit of MATLAB.
    Within the body of the mexFunction definition, full C++ syntax is
    allowed.
    At runtime you have to tell the  executable (in this case the output 
    of cmex) where to find the libraries that resolve the C++ symbols.  
    It needs to know this in order to go look for them at load time.  
    Compile your MEX-file like this:
         g++ -c -I<directory containing mex.h> example.cxx
         cmex example.o -L/hub/lib/sun4 -lg++ -lgcc
    Jeffery Faneuff  April 1, 1994  <jeff@mathworks.com>
    Copyright (c) 1994 The Mathworks, Inc.
*/
#include <iostream.h>
extern "C" {
#include <math.h>
#include "mex.h"
}
/****************************/
class MyData {
public:
        void display();
        void set_data(double v1, double v2);
        MyData(double v1 = 0, double v2 = 0); 
        ~MyData() { }
private:
        double val1, val2;
};
MyData::MyData(double v1, double v2) { val1 = v1; val2 = v2; }
void MyData::display() { 
        cout << "Value1 = " << val1 << "\n";
        cout << "Value2 = " << val2 << "\n\n";
 }
void MyData::set_data(double v1, double v2) { val1 = v1; val2 = v2; }
/*********************/
static
  void example(   double  num1, double num2 )
{
        cout << "\nThe initialized data in object:\n";
        MyData *d = new MyData; // Create a  MyData object
        d->display();           // It should be initialized to zeros
        d->set_data(num1,num2); // Set data members to incoming values
        cout << "After setting the object's data to your input:\n"; 
        d->display();           // Make sure the set_data() worked
        delete(d);
        return;
}
extern "C" {
  void mexFunction(
                   int          nlhs,
                   Matrix       *plhs[],
                   int          nrhs,
                   Matrix       *prhs[]
                   )
    {
     double      *vin1, *vin2;
      
      /* Check for proper number of arguments */
      
      if (nrhs != 2) {
        mexErrMsgTxt("EXAMPLE requires two input arguments.");
      } else if (nlhs >= 1) {
        mexErrMsgTxt("EXAMPLE requires no output argument.");
      }      
      vin1 = mxGetPr(prhs[0]);
      vin2 = mxGetPr(prhs[1]);
      example(*vin1, *vin2);
      return;
    }
}                          /* end of extern "C" */

(c) Copyright 1994 by The MathWorks, Inc.