목표: 행렬 각 원소를 입력 받고 행렬 출력 및 역행렬을 이용한 연립방정식의 해 등을 구하는 함수를 구현해보자.

사전지식:

1. inline keyword http://devgraphy.tistory.com/10

구조체와 클래스를 이용한 행렬 클래스 만들기 프로젝트는 총 6번에 걸쳐서 발전시킬 것이다. 차차 발전시킬 부분이 처음엔 다소 미흡해 보일 수 있으나 포인팅하여 모호함을 최대한 줄이려고 노력하겠다. 최종본을 원하면 (...url...)

앞서 146과 다른 점은 overloaded operator함수를 inline으로 바꾸는 것 이다. 그 이외는 바뀐 게 없다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Matrix
{
private:
    strMat* _matrix;    //structure to dynamcially assign
 
public:
    Matrix(int row,int col)    //class constructor
    {
        _matrix = new strMat(row, col, 0);
    }
    ~Matrix()                    //class destructor
    {
        cout << "Matrix end!" << endl;
        delete _matrix;
    }
    int getRow()const { return _matrix->row; }
    int getCol()const { return _matrix->col; }
    double getVal(int row, int col)const
    {
        return _matrix->mat[row][col];
    }
};
//inline function
inline ostream& operator<<(ostream& ostrm, const Matrix& m)
{
    for (int i = 0; i < m.getRow(); i++)
    {
        for (int j = 0; j < m.getCol(); j++)
        {
            ostrm << m.getVal(i, j) << "\t";
        }
        ostrm << endl;
    }
    return ostrm;
}
 
cs

※ 앞서 friend Keyword가 inline으로 바뀌었는데, friend와 inline은 적절한 비교대상이 아니다. inline 함수는 Normal 함수와 비교되어야 한다.

단지 코드의 성능 개선의 관점에서 바라보면 된다. 그 이외엔 Normal 함수와 다른게 없다.