2015-05-06 47 views
0

我正在嘗試使用c++來重寫java代碼。我根本不精通C++。傳遞對象類型爲std :: array構造函數

原始java代碼

public static <T, K> K[] toArray(ITemplateCommand<T, K> command, List<T> templates) { 
    if (null == templates) { 
     return null; 
    } 
    K[] array = (K[]) Array.newInstance(command.getClassOfK(), templates.size()); 
    for (int i = 0; i < templates.size(); i++) { 
     array[i] = command.buildTemplate(templates.get(i)); 
    } 
    return array; 
} 

c++代碼。

class TemplateImplementation { 

    public: 
     template<class K, class T> 
     static K* toArray(ITemplateCommand<T,K> *command, std::list<T>& templates) { 
      if (nullptr == templates) { 
       return nullptr; 
      } 
      std::array<command->getClassOfK(), templates.size()> arr; // no idea how to pass object type there 
      for(int i = 0; i < templates.size(); i++) { 
       arr[i] = command->buildTemplate(templates[i]); 
      } 
     } 
}; 

java我創建的接口和多種實現方式,其中getClassOfK返回K對象的類。

在這裏爲了簡化事情,我決定不創建實現,但只使用virtual作爲接口的方法。

template<class T, class K> 
class ITemplateCommand { 
public: 
    virtual K* buildTemplate(T* tmplate); 

    virtual std::type_info getClassOfK(); 
}; 

但我在編譯過程中的幾個錯誤(我用網上c++編譯)

sh-4.2# g++ -std=c++11 -o main *.cpp                                                   
main.cpp: In static member function 'static K* TemplateImplementation::toArray(ITemplateCommand<T, K>*, std::list<T>&)':                              
main.cpp:24:64: error: type/value mismatch at argument 1 in template parameter list for 'template<class _Tp, long unsigned int _Nm> struct std::array'                       
      std::array<command->getClassOfK(), templates.size()> arr; // no idea how to pass object type there                                
                   ^                                           
main.cpp:24:64: error: expected a type, got 'command->.getClassOfK()'                                          
main.cpp:24:69: error: invalid type in declaration before ';' token                                           
      std::array<command->getClassOfK(), templates.size()> arr; // no idea how to pass object type there                                
                    ^                                          
main.cpp:26:22: error: invalid types 'int[int]' for array subscript                                           
       arr[i] = command->buildTemplate(templates[i]);                              

但我的主要問題是如何類類型傳遞到的std ::使用std陣列的構造函數: :TYPE_INFO?或者使用這個對象是不可能的?

P.S.此外,我知道這是不返回從函數指針是一個好主意,但我想保持代碼儘可能接近原始

+0

這裏有一個基本的不匹配。 C++模板基於編譯時專業化,因此您無法從運行時值創建模板。 – user1937198

+0

嗯...所以不可能重寫原始代碼? – lapots

+0

這是,你只需要瞭解C++。首先查看std :: vector,以及如何使用C++模板參數。 – user1937198

回答

3

有三件事情錯了這條線:

std::array<command->getClassOfK(), templates.size()> arr; 

array類模板的需求用兩個東西實例化:一個類型和一個可轉換爲size_t的常量表達式。這兩件事需要在編譯時可用templates.size()基於那個list運行時中的元素數,因此將list<T>轉換爲std::array<K, N>是不可能的。第二個問題是getClassOfK()不是一種類型 - 它是一個返回對象的函數 - 完全不符合array的需求。但這是更簡單的問題,您需要傳入K的類型,並且您已經擁有它:它只是K。最後一個問題是,你想返回一個K*和一個array<K,N>而不是一個K*

編寫此代碼的正確方法是使用動態容器。具體來說,std::vector

template<class K, class T> 
static std::vector<K*> toArray(ITemplateCommand<T,K> *command, std::list<T>& templates) { 
    std::vector<K*> vec; 
    vec.reserve(templates.size()); 
    for (T& tmpl : templates) { 
     vec.push_back(command->buildTemplate(tmpl)); 
    } 
    return vec; 
} 
+0

Hm。所以'std :: array arr;'解決方案將無法工作? – lapots

+0

@ user1432980編號'templates.size()'不是一個常量表達式,它只能在運行時進行評估。你*可以*簡單地返回一個簡單的數組('K * arr = new K [templates.size()];'),但是強烈使用'vector'。 – Barry

+0

哦!所以,如果我已經固定'大小'它可以工作?我的意思是在那裏傳遞'K'。 – lapots

相關問題