内联函数作为类方法
Inline function as a class method
我开发了自己的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
函数需要在头文件中实现。如果它确实提高了您的性能,您需要通过基准测试进行检查。
然而,好的编译器可能会自动内联函数(希望如此)。
对于你的问题,我更希望有许多小的功能。它们通常更容易维护,如果正确,可以单独检查。
相关文章:
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 如何制作一个将函数作为参数的类方法
- 函数从唯一代码调用正确的子类方法
- c++类:编写函数的不同方法
- 在何处放置与类方法相关的帮助程序函数C++
- 如何将CLI类的函数指针作为参数传递给C++方法
- 指向类中函数方法的指针不起作用
- 如何调用其对象位于函数内的类方法
- C++ - 类方法函数指针unordered_map的初始化器列表
- 在结构的构造函数中使用类方法赋值变量
- 类方法和全局函数中的别名
- 如何从具有多个对象/字段的类中调用方法函数
- 如何从类构造函数传递值C++作为此类方法的默认参数?
- 对静态重载(类)函数/方法的调用是不明确的
- 如何为其他类成员函数编写模板包装方法
- C++;类方法指针;λ;将 lambda 作为成员函数指针传递;
- 从类方法调用命名空间中名为 Same 的函数时,重载解析失败
- 是否有一种方法可以始终通过值(制作副本)而不是在使用类成员函数时通过参考来传递
- 递归std ::函数定义模板类方法
- 类方法/函数中的指针