Type Conversion Example
Example 4 – Type Conversion
FILENAME: example4.cpp
Program:
/*
* Example program illustrating type conversion. Template functions will n
* not do automatic conversion, but Math.h++ provides explicit casting between
* collection classes of different datatypes.
*/
 
// Include the RWMathVec class header files.
 
#include <rw/math/mathvec.h>
#include <iostream>
 
// Initial data for the vectors.
double adata[] = {1., 3., -2., -6., 7.};
int idata[] = {2, 6, -4, 2, 1};
 
int main()
{
// Construct the vectors V and iV from the
// arrays defined above.
 
RWMathVec<double> V(adata, 5);
RWMathVec<int> iV(idata, 5);
 
/*
* Output the dot product of the two vectors.
* Note that the function dot has prototype
* template<class T> dot(RWMathVec<T>&, RWMathVec<T>&),
* so to use dot, the RWMathVec<int> is cast to
* RWMathVec<double> to get a match.
*/
RWMathVec<double> temp = RWConvertMathVec<int,double>(iV);
std::cout << dot (V, RWConvertMathVec<int,double>(iV));
}
 
Sample Input:
None required.
Sample Output:
23