内联函数作为类方法

Inline function as a class method

本文关键字:类方法 函数      更新时间:2023-10-16

我开发了自己的Matrix类。构造函数从文件中读取矩阵。基质有自由细胞和"壁"。构造函数还读取广度优先搜索的开始点和结束点(以查找从start_point到finish_point的最短路径)。这是标题代码:

//MyMatrix.h文件

#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文件

...
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;
}
...

我想定义下一步算法的相邻单元是否空闲。当然,我可以使用这样的代码:

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...
    ... 
}

但它看起来很难看。我将为左单元格、上单元格和下单元格添加这样的检查。我想实现内联函数(比如:inline int IS_RIGHT_NEIGHBOUR_REACHABLE(Field_Point_Type Current_Point);)。

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

它看起来好多了!但我以前从未使用过这样的内联函数定义,并且意外地发现了它。它的编程风格好吗?在我的类中开发简单的int IS_RIGHT_NEIGHBOUR_REACHABLE(Field_Point_Type Current_Point);方法更好吗?离开这样的支票更好吗:

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...
    ... 
}

我认为我们还没有一个既定的"好风格"。能够从单独编译的.cpp文件中内联函数的编译器是最流行的编译器的最新模型。

直到几年前,您还必须将所有内联函数都放在.h文件中,这样编译器才能在编译调用时看到它。如果您的编译器不是最新的型号,那么这可能仍然是规则。

inline函数需要在头文件中实现。如果它确实提高了您的性能,您需要通过基准测试进行检查。

然而,好的编译器可能会自动内联函数(希望如此)。

对于你的问题,我更希望有许多小的功能。它们通常更容易维护,如果正确,可以单独检查。