2012-04-10 5 views
0

C++에서 Commands (파일 이름 : commands.cpp)라는 클래스를 만들었습니다.클래스 배열에 함수를 호출하는 방법 C++

나는 그것을 취하여 명령 배열 (파일 이름 test.cpp)에 넣었다.

내가 알고 싶은 것은 Commands 클래스에있는 함수를 호출하는 방법입니다.

예를 들어 나는

 void command::init(char data[]) 
     { 
     //detail 
     } 

무엇을 내가 함수를 호출 할 시도라고 명령 클래스 내에서 기능을한다이

편집

Class test{ 
    int CmdCount;  // number of commands in the array 
    int MaxCmds;  // max amount of commands allowed 
    command* cmds; 
    Public: 
    int get_command_count() const{ 
      return CmdCount; 
    } 

    int readfile(const char fname[]){ 
      char line[161]; 

      FILE* fp; 
      fp = fopen(fname, "r"); 

      if(fp){ 
       for(int i = 0; 1 == fscanf(fp, "%160[^\n]\n", line; i++){ 
        cmds[get_command_count()].init(line); 
        CmdCount += 1; 
       } 
      } 
      fclose(fp); 
    } 
    }; 

난 그냥 void 명령을 호출 할 수있는 방법을 알고 싶어요 : : it (char data []).

제안 사항?

감사합니다.

+0

무엇이 잘못 되었나요? 즉,'cmds [get_command_count()]. ​​init (line);' –

+0

은'init' 클래스의 public 메소드입니다.'line'이'char *'이고'cmds'가 'get_command_count()'는 유효한 배열 인덱스를 반환합니다. – keety

+0

'get_command_count'는 유효한 명령의 총수 *를주는 것처럼 들립니다.이 경우 유효한 명령이 아닌 첫 번째 인덱스입니다. – celtschk

답변

1

배열과 같은 사운드에는 클래스의 인스턴스가 포함되어 있습니다. 이 경우 당신은 배열의 단일 항목에 메소드를 호출 할 :

my_array[i].someMethod(); 

하는 my_array[i]이 클래스의 인스턴스입니다.

관련 문제