Hey guys, I am trying to understand this code our lecturer wrote..
I have understood other use of exceptions, but I can't understand his
So I need your help
Code:
#pragma once
#include <exception>
#include <iostream>
#include <fstream>
#include <exception>
using namespace std;
class MatrixException: public exception
{
public:
virtual const char * what() const throw();
};
class DynamicMatrix
{
public:
float * m;
int rows, cols;
public:
DynamicMatrix(int rows, int cols);
DynamicMatrix(DynamicMatrix & m);
~DynamicMatrix(void);
void set(int row, int col, float val);
float get(int row, int col) const;
DynamicMatrix operator*(const DynamicMatrix & matrix) const throw (MatrixException);
DynamicMatrix operator=(const DynamicMatrix & matrix);
friend ostream &operator<<(ostream &stream, DynamicMatrix v);
friend istream &operator>>(istream &stream, DynamicMatrix &v);
};
#include "DynamicMatrix.h"
#include <iostream>
using namespace std;
const char * MatrixException::what() const throw()
{
return "Could not multiply these matrices";
}
DynamicMatrix DynamicMatrix::operator*(const DynamicMatrix & matrix) const throw (MatrixException)
{
if (this->cols != matrix.rows)
{
throw MatrixException();
}
DynamicMatrix ret(rows, matrix.cols);
for (int row = 0 ; row < rows ; row ++)
{
for (int col = 0 ; col < matrix.cols ; col ++)
{
float value = 0.0f;
for (int i = 0 ; i < matrix.cols ; i ++)
{
value += get(row, i) * matrix.get(i, col);
}
ret.set(row, col, value);
}
}
return ret;
}
I have been able to understand that he defined a MatrixException class which overrides the what() function in the base class so that he can change its use for his own purpose..
what I don't understand is how it is declared?
Code:
virtual const char * what() const throw();
virtual, to tell us he is overriding the what() from the base class.
const char * i think means what() returns a pointer to a constant character(or first character in a string).
const throw() I don't know what that means...
I have seen throw, but throw() - it is a function now?
what does const throw() after what() even mean? so what does the definition above mean?
Code:
const char * MatrixException::what() const throw()
{
return "Could not multiply these matrices";
}
so there he is defining how the what() should be used..but what does the const throw() beside it mean? haha
then here he does
Code:
DynamicMatrix DynamicMatrix::operator*(const DynamicMatrix & matrix) const throw (MatrixException)
{
if (this->cols != matrix.rows)
{
throw MatrixException();
}
DynamicMatrix ret(rows, matrix.cols);
for (int row = 0 ; row < rows ; row ++)
{
for (int col = 0 ; col < matrix.cols ; col ++)
{
float value = 0.0f;
for (int i = 0 ; i < matrix.cols ; i ++)
{
value += get(row, i) * matrix.get(i, col);
}
ret.set(row, col, value);
}
}
return ret;
}
there he passes the MatrixException class (I don't think it is an object anyways) to the throw function..but how does link with the const throw(MatrixException) in the function declaration?
when he does this throw MatrixException() what happens then?
Can anyone explain this...s***
thanks!