矢量據我所知,const_cast會刪除對象的常量性是不好的,const_cast會與對象
我有以下的使用情況下,
//note I cannot remove constness in the foo function
foo(const std::vector<Object> & objectVec) {
...
int size = (int) objectVec.size();
std::vector<Object> tempObjectVec;
//Indexing here is to just show a part of the vector being
//modified
for (int i=0; i < (int) size-5; ++i) {
Object &a = const_cast<Object&> objectVec[i];
tempObjectVec.push_back(a);
}
foo1(tempObjectVec);
}
如果我更改了foo1 tempObjectVec對象,將原來的對象在ObjectVec中的變化,我說是因爲我傳遞引用,進一步是這樣高效。你能否提出其他建議?
編輯的代碼,根據您的建議。 – kal 2009-01-06 02:32:16