gpt4 book ai didi

c++ - 作为类方法的内联函数

转载 作者:搜寻专家 更新时间:2023-10-31 01:16:36 25 4
gpt4 key购买 nike

我开发了自己的 Matrix 类。构造函数从文件中读取矩阵。矩阵有自由细胞和“墙”。构造函数还读取广度优先搜索的起点和终点(以找到从 Start_point 到 Finish_Point 的最短路径)。这是 header 代码:

//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 文件中,这样编译器才能在编译调用时看到它。如果您的编译器不是最新型号,那可能仍然是规则。

关于c++ - 作为类方法的内联函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8963633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com