0
欲一個類(B)(C-傳統接口)的陣列複製到使用std另一個類(A)的矢量::複製。 能正常工作,當A提供了一種構造與參數B,參見以下的代碼:轉換數組,使用std ::複製
#include <algorithm>
#include <vector>
struct B
{
};
struct A
{
A(){}
#if 1
A(const B& b)
{
// constructor converting B into A
}
#endif
};
A toA(const B& b)
{
A rc;
//free function converting B into A
return rc;
}
int main()
{
std::vector<A> VecA;
B* arrayB;
size_t elementsOfB;
//here fill array B and elmentsOfB in C legacy code - does not matter for minimum example
//now copy array of B into vector of A
std::copy(arrayB, arrayB + elementsOfB, std::back_inserter(VecA));
return 0;
}
的例子也可作爲Live Example。
我想刪除的轉換構造函數,而不是我想使用免費的功能「TOA」。當然,這並不編譯(試行的「#if 0」,在A的結構定義),因爲性病::複製都想有一個直接的功能來做到這一點。
有沒有辦法使用std ::有免費的功能複製?