Lucky Man
Lucky Man

Reputation: 1518

Inline function as a class method

I developed my own Matrix class. Constructor reads a matrix from file. Matrix has free cells and "walls". Also constructor reads start and finish points for Breadth first search (to find the shortest way from Start_point to Finish_Point). Here is code of header:

//MyMatrix.h file

#ifndef __MYMATRIX_H__
#define __MYMATRIX_H__

#include <tchar.h>
#include <iostream>
#include <deque>

//using namespace std;
#define MAX_MATRIX_SIZE 1000

#define FREE_CELL_SIGNIFICATION '0'
#define BALL_SIGNIFICATION 'B'
#define UP_SIGNIFICATION 'U'
#define DOWN_SIGNIFICATION 'D'
#define LEFT_SIGNIFICATION 'L'
#define RIGHT_SIGNIFICATION 'R'
#define START_POINT_SIGNIFICATION 'S'
#define FINISH_POINT_SIGNIFICATION 'F'

typedef std::pair<int,int> Field_Point_Type;

//#define IS_RIGHT_NEIGHBOUR_REACHABLE(Current_Point) (((Current_Point.second+1) <= Column_Count)&&((Matrix_Field[Current_Point.first][Current_Point.second+1]==FREE_CELL_SIGNIFICATION)||(Matrix_Field[Current_Point.first][Current_Point.second+1]==FINISH_POINT_SIGNIFICATION))) ? true : false;



class Matrix {
    private:
        int Column_Count; //Cols
        int Row_Count;//Rows
        char** Matrix_Field;
        Field_Point_Type Start_Point;
        Field_Point_Type Finish_Point;
        bool Matrix_Is_Correct;
    public:
        Matrix(_TCHAR* Input_File_Name);
        int Breadth_first_search(unsigned int Start_X,unsigned int Start_Y,unsigned int Finish_X,unsigned int Finish_Y);
        ~Matrix();
        inline int IS_RIGHT_NEIGHBOUR_REACHABLE(Field_Point_Type Current_Point);
};

//MyMatrix.cpp file

...

inline int Matrix::IS_RIGHT_NEIGHBOUR_REACHABLE(Field_Point_Type Current_Point)
{      
    return  (((Current_Point.second+1) <= Column_Count)&&((Matrix_Field[Current_Point.first][Current_Point.second+1]==FREE_CELL_SIGNIFICATION)||(Matrix_Field[Current_Point.first][Current_Point.second+1]==FINISH_POINT_SIGNIFICATION))) ? true : false;
}

...

I'd like to define are the neighbour cells free for the next step of algorithm. Of course I can use such code for this:

if (((Current_Point.second+1) <= Column_Count)&&((Matrix_Field[Current_Point.first][Current_Point.second+1]==FREE_CELL_SIGNIFICATION)||(Matrix_Field[Current_Point.first][Current_Point.second+1]==FINISH_POINT_SIGNIFICATION))) {
    //Adding of right cell to deque...
    ... 
}

but it looks ugly. I am going to add such checks for left, up and down cells. I'd like to implement inline functions (like this: inline int IS_RIGHT_NEIGHBOUR_REACHABLE(Field_Point_Type Current_Point);).

if (IS_RIGHT_NEIGHBOUR_REACHABLE(Current_Point)) {
    //Adding of right cell to deque...
    ... 
}

It looks much better! But I haven't use such definition of inline function before and discovered it accidentally. Is it good programming style? Is it better to develop simple int IS_RIGHT_NEIGHBOUR_REACHABLE(Field_Point_Type Current_Point); method within my class? Is it better to leave such check:

if (((Current_Point.second+1) <= Column_Count)&&((Matrix_Field[Current_Point.first][Current_Point.second+1]==FREE_CELL_SIGNIFICATION)||(Matrix_Field[Current_Point.first][Current_Point.second+1]==FINISH_POINT_SIGNIFICATION))) {
    //Adding of right cell to deque...
    ... 
}

Upvotes: 2

Views: 196

Answers (2)

Bo Persson
Bo Persson

Reputation: 92391

I don't think we have an established "good style" yet. Compilers capable of inlining functions from a separately compiled .cpp file are rather recent models of the most popular compilers.

Until a couple of years ago you had to have all inline functions in a .h file, so the compiler could see it while compiling the call. If your compiler is not the latest model, that might still be the rule.

Upvotes: 3

tune2fs
tune2fs

Reputation: 7705

inline Functions need to be implemented in header files. If it really improves your performance you need to check by benchmarks.

However good compilers might inline functions automatically (hopefully).

To your question, I would prefer to have many small functions. They are normally easier to maintain and can be checked individually if they are correct.

Upvotes: 1

Related Questions