Using the C++ Math Library    

How C++ and the Library Differ from MATLAB

The most obvious difference between C++ and MATLAB is the syntax. Many of MATLAB's neat syntactic tricks, such as : and ', are not valid C++ syntax, and therefore are available only through function calls in C++. Though the lack of operators may at first seem to be the biggest difference, it is not. Since each of the operators has an equivalent function, the only thing missing is convenience of syntax.

A much bigger difference is the lack of multiple return values in C++. A C++ function returns either zero or one value. To simulate MATLAB's multiple return values, the MATLAB C++ Math Library requires that you pass all but the first of your return values as inputs to the function; you must pass these arguments as pointers to arrays so that the called function can modify them.

The differences between the flow of control statements (if-statements, for- and while-loops) in MATLAB and C++ are minor. Certainly the syntax is a bit different, but the most important difference is that the arguments to these statements in C++ must be scalars. Because if and while require Boolean values, you must use the MATLAB C++ Math Library tobool() function to reduce any array that appears in one of these statements to a scalar. tobool() returns a Boolean result.

Variable declaration, required by C++, is another difference, but a minor one, especially since C++ allows you to declare variables at any point in the program. Also, the C++ compiler will forcefully remind you of any variables you have forgotten to declare.

C++ and MATLAB both support try and catch blocks, which allow you to detect and recover from an error. However, C++'s exception-handling mechanism is somewhat more comprehensive than MATLAB's error handling because you can associate an object with a catch block. In MATLAB, a catch block catches any error.

A C++ exception is an object created when an error occurs. The exception typically identifies the type of error and its location. Like a MATLAB error, an unhandled C++ exception will terminate the program. Unlike a MATLAB error, you won't get to see the error message associated with the exception unless you catch the exception and print it out manually -- C++ will not do this for you automatically.

See Differences Between C++ and MATLAB in Chapter 9 for more details on the differences between C++ and MATLAB.


 How the Library Is Similar to MATLAB MATLAB C++ Math Library Basics