2014-10-18 3 views
0

Так в Java я имел следующую реализацию:полиморфизм и наследование в C++

public abstract class PFigure implements Comparable 

и реализацию Java для моего наследования и полиморфизма:

public class PFigureList 
{ 
    private final int MAX_VEHICLES = 9; 
    private PFigure list[] = new PFigure[MAX_VEHICLES]; 
    private int count = 0; 

    /** 
    Adds a PFigure to the list if the list is not full and increments the count. 

    @param myFig The "vehicle" PFigure that is going to be added to the list 
    */ 
    public void add(PFigure myFig) 
    { 
     if(count <= 9) 
     list[count++] = myFig; 
    } 
/** 
    For every figure in the list, it calls their hide() function, their 
    polymorphic move() function, and then their draw() function to show where 
    they are now. 
    */ 
    public void move() 
    { 
     for(int i = 1; i < count; i++) 
     { 
     list[i].hide(); 
     list[i].move(); 
     list[i].draw(); 
     } 
    } 

Теперь то, что я хочу сделать это за исключением очень похоже на него в C++. Вот мой код:

void VBotList::Add(VBot * add) 
{ 
    vbot[count++] = add; 
} 

void VBotList::Move() 
{ 
    /*for(int i = 0; i < list.GetCount(); i++) 
     list.GetValue(i)->Move();*/ 

    for(int i = 0; i < count; i++) 
     vbot[i]->Move(); 
} 

class VBotList 
{ 
    private: 
    int count; 

    VBot * vbot[50]; 

    public: 
     VBotList() : count(0){ } 
     void Add(VBot * vbot); 
     void Move(); 
     void Show(); 

}; 

public ref class MyForm : public System::Windows::Forms::Form 
{ 
public: 
    MyForm(void) 
    { 
     InitializeComponent(); 
     // 
     //TODO: Add the constructor code here 
     // 
    } 
    static VBotList * list = new VBotList(); 

И моя попытка реализовать:

private: System::Void speedTimer_Tick(System::Object^ sender, System::EventArgs^ e) { 
       list->Move(); 
       Invalidate(); 
       speedTimer->Interval = speedTrackBar->Value; 
      } 
private: System::Void vbotAddButton_Click(System::Object^ sender, System::EventArgs^ e) { 
      if(comboBox1->SelectedIndex == 1) 
      { 
       VBot * x = new BillyBot(System::Convert::ToInt32(textBox1->Text), System::Convert::ToInt32(textBox2->Text), panel1); 
       list->Add(x); 
      } 
     } 
private: System::Void panel1_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e) { 
      list->Show(); 
     } 

Моя цель здесь должна была взять объект VBot, который хранится в VBotList, который был, как мой PFigureList объектов PFigure в java. Я не знаю, почему, но я не могу заставить его фактически отображать мой объект на панели. Мне пришлось инициализировать статическую версию VBotList, чтобы она не выдавала мне сообщения об ошибках. Я пропустил что-то совершенно очевидное или я просто делаю что-то неправильное в отображении объекта? Любые подсказки, советы или пощечины на руке о моем коде будут замечательными.

Показать() В основном просто отображается изображение. При необходимости отправлю код.

ответ

1

Вы работаете в C++/CLI, так что вы класс должен управляться также, например:

ref class Bot 
{ 
public: 
    Bot() 
    { 

    } 
}; 

ref class VBotList 
{ 
private: 
    int m_count; 
    array<Bot ^> ^vbot; 
public: 
    VBotList() : m_count(0), vbot(gcnew array<Bot ^>(50)) 
    { 
    } 
    void Add(Bot ^newBot) 
    { 
     vbot[m_count++] = newBot; 
    } 
    int getCount() 
    { 
     return m_count; 
    } 
}; 

И дальше:

private: 
    VBotList ^list; 

public: 
    Form1(void) 
    { 
     InitializeComponent(); 
     list = gcnew VBotList(); 
    label1->Text = System::Convert::ToString(list->getCount()); 
    } 

// ... 
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) 
{ 
    Bot ^bot = gcnew Bot(); 
    list->Add(bot); 
    label1->Text = System::Convert::ToString(list->getCount()); 
} 

^объявить дескриптор управляемого указателя.

Смежные вопросы