作为类方法的内联函数

发布于 2024-12-28 19:47:51 字数 2993 浏览 1 评论 0原文

我开发了自己的 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...
    ... 
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴情换悲伤 2025-01-04 19:47:51

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

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

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.

寄意 2025-01-04 19:47:51

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文