昨天我看了同事的一些代码,发现了这个。
class a_class
{
public:
a_class() {...}
int some_method(int some_param) {...}
int value_1;
int value_2;
float value_3;
std::vector<some_other_class*> even_more_values;
/* and so on */
}
a_class a_instances[10];
void some_function()
{
do_stuff();
do_more_stuff();
memset(a_instances, 0, 10 * sizeof(a_class)); // <===== WTF?
}
Is that legal (the WTF line, not the public attributes)? To me it smells really, really bad...
The code ran fine when compiled with VC8, but it throws an "unexpected exception" when compiled with VC9 when calling a_instances[0].event_more_values.push_back(whatever)
, but when accessing any of the other members. Any insights?
EDIT: Changed the memset from memset(&a_instances...
to memset(a_instances...
. Thanks for pointing it out Eduard.
EDIT2: Removed the ctor s return type. Thanks litb.
结论:谢谢大家,你们证实了我的怀疑。