2012-01-22 3 views
2

내 자신의 Matrix 클래스를 개발했습니다. 생성자는 파일에서 행렬을 읽습니다. 매트릭스에는 자유 세포와 "벽"이 있습니다. 또한 생성자는 폭 넓은 첫 번째 검색 (Start_point에서 Finish_Point까지 최단 경로 찾기)의 시작 및 끝 지점을 읽습니다. 여기 헤더의 코드입니다 :클래스 메서드로 인라인 함수

#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이 파일

//MyMatrix.h 파일

내가 정의하고 싶습니다
... 

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

CPP 파일에 인라인 함수를 구현하지 마십시오 !! – Goz

+1

(OT : [친구들]이나 다른 인사말을 게시하지 마십시오.] (http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed- from-posts), C++을 맨 앞에 놓지 않으면, 가장 높은 태그가 자동적으로 페이지의'title'에 추가됩니다. 거기에 C++을 넣으려면, 제목 문장에 그것을 흐르게하십시오.) – Mat

+0

OK, Mat. 이렇게 돼서 미안하다. –

답변

3

저는 아직 "좋은 스타일"이 있다고 생각하지 않습니다. 별도로 컴파일 된 .cpp 파일의 함수를 인라인 할 수있는 컴파일러는 최근 인기있는 컴파일러 모델입니다.

몇 년 전까지 만해도 컴파일러는 .h 파일의 모든 인라인 함수를 가져야 만 호출을 컴파일 할 수있었습니다. 컴파일러가 최신 모델이 아니라면 여전히 규칙 일 수 있습니다.

1

inline 기능은 헤더 파일에서 구현 될 필요가있다. 성능이 실제로 향상되면 벤치 마크를 통해 확인해야합니다.

그러나 좋은 컴파일러는 자동으로 (잘하면) 인라인 함수를 사용합니다.

귀하의 질문에, 나는 많은 작은 기능을 선호합니다. 일반적으로 유지 관리가 쉽고 올바른 경우 개별적으로 확인할 수 있습니다.

관련 문제