当使用原始指针时,我能够做到这一点:
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;
)是正确的吗?
放轻松
std::vector<Vector3> vertices(amount);
assert(!vertices.empty());
fread(&vertices[0], sizeof(Vector3), amount, file);