使用原始指针时,我能够做到这一点:
std::vector< Vector3* > vertices;
Vector3* v = new Vector3[amount];
fread(v, sizeof(Vector3), amount, file);
for ( int i = 0; i < amount; ++i )
vertices.push_back( &v[i] );
现在,如何使用智能指针做同样的事情?我尝试过这样的事情:
std::vector< std::unique_ptr< Vector3 > > vertices;
std::unique_ptr< Vector3 >* v = new std::unique_ptr< Vector3 >[amount];
fread(v, sizeof(Vector3), amount, file);
for ( int i = 0; i < amount; ++i )
vertices.push_back( std::move( v[i] ) );
delete [] v;
它有什么意义吗?它编译,它的工作原理,但我担心最后一条指令(delete [] v;
)是正确的吗?
答案 0 :(得分:2)
放轻松
std::vector<Vector3> vertices(amount);
assert(!vertices.empty());
fread(&vertices[0], sizeof(Vector3), amount, file);