Comparing C++ and MATLAB Code
The following is a set of notes on the difference between the C++ and MATLAB code.
Contents
Data Members and Properties
- Data members and properties. C++ data members are implemented as MATLAB class properties and specified in a properties block of the class definition file.
- C++ class data member names. C++ class data members which use the convention m_name have been left with the same property name in the M code to aid the comparison, but this convention is not required in MATLAB as obj.name syntax makes class properties obvious.
- C++ data member access. C++ data members with simple public access are implemented as public properties in MATLAB. There is no need to create an access method.
22 properties 23 m_bitDelay % Instead of public access method 24 end
- C++ access methods. C++ data member access methods that do carry out some operation such as validate the property, have been implement as a MATLAB get or set method, such as this get method for the m_bitDelay property.
138 function out=get.m_bitDelay(hObj)
139 switch hObj.m_bitrate
140 case 1
141 out=1;
142 case 11
143 out= constants.codeLength;
144 otherwise
145 error(''); % Should not occur
146 out=0;
147 end
148 end
- C++ data member access control. MATLAB property attributes have been used to control access to properties. Although its best to leave properties public during development and debugging to ease access. Note that you need to set the GetAccess and SetAccess attributes. For example:
19 properties (GetAccess='private', SetAccess='private') 20 m_dB_noise; 21 m_x1; 22 m_x2; 23 m_x3; 24 m_x4; 25 m_x5; 26 m_x6; 27 end
- Data member access within methods. The first input argument of a class method is typically the object (e.g. obj) but does not have to be. Properties inside of methods are accessed via the object e.g. obj.property or hObj.property convention. Methods that modify property values need to pass the object back as a return argument from the method if it is a value class but not if it is a handle class.
Methods and Functions
- Methods. C++ member functions are implemented as MATLAB class methods and specified as MATLAB functions in a methods block of the class definition file
- Method calling. Functional notation is mainly used for calling methods in the application e.g. method(obj, arg1, arg2), but you could use dot notation e.g. object.method(arg1, arg2) to more closely match C++ code.
- Static functions accessed by more than one class. These have been placed on the path such as the setRSRCpulseShapingFilter.
- Indexing operators. Overloaded indexing operators in C++ have been implemented in MATLAB with subsref and subsasgn methods. These are called when an object is access by either regular indexing (), cell array indexing {} or accessed with a dot ..
98 function out=subsref(hObj,s)
106 switch s.type
107 case '()' % Subs ref
108 if length(s.subs)==1
109 index = s.subs{1};
110 out=hObj.m_fc(index);
Other Topics
- C++ pointers. MATLAB handle classes are used to give objects reference behavior, similar to the behavior of C++ objects typically accessed via pointers. Most classes in the application are specified as handle classes, by inheriting from the handle base class in the classdef line.
11 classdef AWGN < handle
- C++ loop iterators. In general, C++ loop iterators i and j are have been replaced in the example with k, ii or jj to avoid confusion with the complex values i and j in MATLAB.
- C++ copy constructors. These are not required in MATLAB code
- C++ destructors. These are not required in the MATLAB code, but customized ones could be specified for handle classes
- C++ templates. There is no equivalent to templates in MATLAB but this is not as necessary, as MATLAB is dynamically typed
- Virtual methods in base classes. These are not required.
