2016-12-31 76 views
0

我有一個基類(Tableau)與一個函數(稱爲更新),並在該函數內,我調用另一個函數稱爲updateCustom。重寫和繼承

我想用我的JeuTaquin類來增強我的Base類,並且能夠覆蓋JeuTaquin中的updateCustom函數。因此,在我主要使用JeuTaquin對象調用函數更新時,我想從JeuTaquin開始我的updateCustom,但我不知道該怎麼做!

這裏是內部的Tableau我的更新功能:

template<class T> 
void Tableau<T>::update() 
{ 
int p1 = 1, p2 = 1; 
int currentTurn = 1; 
while(currentTurn!=tour || tour == 0){ 


    cout<<*this<<endl; 

    updateCustom(getInput()); //HERE I CALL MY UPDATECUSTOM FUNCTION 
    if(p2 == 1) 
     computerTurn(); 
    else 
     cout<<"player 2 game end"<<endl; 

    p1 = endTurn(plateau1); 
    p2 = endTurn(plateau2); 
    if(endCheck(p1)) 
     break; 
    currentTurn++; 
} 
cout<<"game ended"<<endl; 
} 

在我的課的畫面,我的功能updateCustom是空的(但聲明)。在我JeuTaquin類,我重寫我的updateCustom功能如下:(但它其實並不重要,看看這是什麼函數內)

template<class T> 
void Tableau<T>::updateCustom(char input) 
{ 
    int i, j; 
    Case<T> *neighbours; 
    while(true) 
    {  
     neighbours = Tableau<T>::checkNeighbours(Tableau<T>::plateau1, i, j); 
     if(input == 'z' && neighbours[0] !=nullptr) 
     { 
      swap(plateau1[i][j],plateau1[i-1][j]); 
      cout<<"Mouvement OK"<<endl; 
      break; 
     } 
     else if(input == 'd' && neighbours[1] !=nullptr) 
     { 
      swap(plateau1[i][j],plateau1[i][j+1]); 
      cout<<"Mouvement OK"<<endl; 
      break; 
     } 
     else if(input == 's' && neighbours[2] !=nullptr) 
     { 
      swap(plateau1[i][j],plateau1[i+1][j]); 
      cout<<"Mouvement OK"<<endl; 
      break; 
     } 
     else if(input == 'q' && neighbours[3] !=nullptr) 
     { 
      swap(plateau1[i][j],plateau1[i][j-1]); 
      cout<<"Mouvement OK"<<endl; 
      break; 
     } 


     cout<<"Mouvement IMPOSSIBLE"<<endl; 
    } 



} 

我無法找到互聯網上的任何提示,當我跑我的更新函數與JeuTaquin對象一起使用,它從我的父類中運行我的空updateCustom,而不是從我的子類(JeuTaquin)中更新我的updateCustom。謝謝你的幫助。

+0

是''updateCustom' virtual'? – Unimportant

+0

不,updateCustom不是虛擬的 –

+0

嗯,應該是,讀了'虛擬'。 – Unimportant

回答

0

我在我的updateCustom函數中添加了虛函數(僅在我的.h中),它的工作原理!當我重新更新定製,I型

void JeuTaquin<T>::updateCustom(char input) 

代替

void Tableau<T>::updateCustom(char input)