xiaofeng.ustc
BAN USERThe major problem is the abuse of memset here. It erased the whole Foo object and it destroyed VPTR in Foo. So when you call virtual function, it will crash.
- xiaofeng.ustc January 28, 2013bool findSmallestTwo(const std::vector<int>& array, int& first, int& second)
{
const int kNumElements = static_cast<int>(array.size());
if (kNumElements <= 1) return false;
first = (array[0] <= array[1]) ? array[0] : array[1];
second = (array[0] <= array[1]) ? array[1] : array[0];
for (int index = 2; index < kNumElements; index++)
{
int value = array[index];
if (value >= second) continue;
else
{
if (value >= first)
{
second = value;
}
else
{
second = first;
first = value;
}
}
}
return true;
}
class Vehicle{}
class Car: public Vehicle {}
class Motor: public Vehicle{}
class Slot
{
Vehicle* pVehicle;
}
class Vehicle{}
class Car: public Vehicle {}
class Motor: public Vehicle{}
class Slot
{
Vehicle* pVehicle;
}
TestMap::iterator iter = test.begin();
int index = 1;
while(iter != test.end())
{
if (index % 2 == 0) iter = test.erase(iter);
else ++iter;
index++;
}
This is correct. It is using anonymous parameter and default parameter both here, even the parameter will not be used in the function body, the function itself has no problem.
- xiaofeng.ustc January 30, 2013