c++ - Inline function as a class method -
c++ - Inline function as a class method -
i developed own matrix class. constructor reads matrix file. matrix has free cells , "walls". constructor reads start , finish points breadth first search (to find shortest way start_point finish_point). here 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) { homecoming (((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 define neighbour cells free next step of algorithm. of course of study can utilize such code 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 deque... ... }
but looks ugly. going add together such checks left, , downwards cells. i'd 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 deque... ... }
it looks much better! haven't utilize such definition of inline function before , discovered accidentally. programming style? improve develop simple int is_right_neighbour_reachable(field_point_type current_point);
method within class? improve 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 deque... ... }
i don't think have established "good style" yet. compilers capable of inlining functions separately compiled .cpp file rather recent models of popular compilers.
until couple of years ago had have inline functions in .h file, compiler see while compiling call. if compiler not latest model, might still rule.
c++ class inline
Comments
Post a Comment