2013-04-25 106 views
1

我已經寫了下面的代碼。功能func()打印標題和數據。函數返回通用類型數據

class ICell 
{ 
    public: 
     wstring header; 
     virtual void Fetch() = 0; 
}; 

template <class T> 
class Cell : public ICell 
{ 
public: 
    //wstring header; 
    T data; 
    void Fetch() 
    { 
     wcout<< header << L": "; 
     cout<<data<<endl; 
    } 
    // implementation of cell methods 
}; 

class Row 
{ 
public: 
    vector <ICell *> cells; 
}; 

有什麼辦法可以在函數中返回數據而不是打印嗎?如果是這樣,哪部分代碼應該被修改? 在此先感謝。

int main() 
{ 
    Cell<int>c1; 
    Cell<double>c2; 

    c1.header = L"Roll", c1.data = 100; 
    c2.header = L"CGPA", c2.data = 3.5; 

    Row r; 
    r.cells.push_back(&c1); 
    r.cells.push_back(&c2); 

    vector <ICell *>::iterator it; 
    for(it=r.cells.begin();it!=r.cells.end();it++) 
    { 
     //checkt type of it wherther it points Cell<int> or Cell<double> 
    } 

    return 0; 
} 

我在這裏改變了我的問題。在循環內部的main()中,我如何檢查'it'指向的對象類型?

謝謝大家的耐心和幫助我:)

+0

將'void Fetch();'改爲'T Fetch();' – 2013-04-25 11:44:53

+0

您可能會返回一串正在輸出的數據,也許? – 2013-04-25 11:44:56

+0

@AlokSave:如果'T'和'header'都是作爲一個單元返回的話,則不應該... – 2013-04-25 11:45:30

回答

2

最簡單的方法是使用dynamic_cast

vector <ICell *>::iterator it; 
for(it=r.cells.begin();it!=r.cells.end();it++) 
{ 
    Cell<int>* cell_i= dynamic_cast<Cell<int>*>(*it); 
    if(cell_i) 
    { 
     do_something(cell_i->data); 
     continue; 
    } 

    Cell<double>* cell_d= dynamic_cast<Cell<double>*>(*it); 
    if(cell_d) 
    { 
     do_something(cell_d->data); 
     continue; 
    } 
} 

更好的辦法是使用訪問者模式:

class ICellVisitor; //declaration for ICell to understand ICell* pointer 
class ICell 
{ public: 
    ~ICell(){}; // important 
    std::wstring header; 
    virtual void visit(ICellVisitor* v) = 0; 
}; 
template <class T> class Cell; // for Cell<T>* pointer 
class ICellVisitor 
{ public: 
    virtual void visit(Cell<int>* c) = 0; 
    virtual void visit(Cell<double>* c) = 0; 
    virtual void visit(Cell<float>* c) = 0; 
    virtual void visit(Cell<long long>* c) = 0; 
}; 
template <class T> class Cell : public ICell 
{ public: 
    //wstring header; 
    T data; 
    void visit(ICellVisitor* v) 
    { 
     std::wcout<< header << L": "; 
     v->visit(this); 
    } 
    // implementation of cell methods 
}; 
class Row 
{ public: 
    std::vector <ICell *> cells; 
}; 

現在,我們需要定義具體的訪客保持每種類型的算法:

class MyCellVisitor: public ICellVisitor 
{ public: 
    void visit(Cell<int>* c){ 
     std::wcout<<"(int)"<<c->data<<std::endl; 
    } 
    void visit(Cell<double>* c){ 
     std::wcout<<"(double)"<<c->data<<std::endl; 
    } 
    void visit(Cell<float>* c){ 
     std::wcout<<"(float)"<<c->data<<std::endl; 
    } 
    void visit(Cell<long long>* c){ 
     std::wcout<<"(long long)"<<c->data<<std::endl; 
    } 
}; 

int main() 
{ 
    Cell<int>c1; 
    Cell<double>c2; 
    c1.header = L"Roll", c1.data = 100; 
    c2.header = L"CGPA", c2.data = 3.5; 
    Row r; 
    r.cells.push_back(&c1); 
    r.cells.push_back(&c2); 
    MyCellVisitor visitor; 
    std::vector <ICell *>::iterator it; 
    for(it=r.cells.begin();it!=r.cells.end();it++) 
    { 
     (*it)->visit(&visitor); 
    } 
    return 0; 
} 
+0

謝謝麻煩。特別爲新的模式(對我來說)代碼。 – 2013-04-25 13:03:32