Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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?
- }
- ...
- for(size_t i=0; i < 10; i++)
- objects[i].clear();
- void clear() {
- a_object o;
- o.swap(*this);
- }
- void clear()
- {
- value1=0;
- value2=0;
- value_3=0f;
- even_more_values.clear();
- }
- class a_class
- {
- public:
- a_class() {...}
- ~a_class() { /* make sure that even_more_values gets cleaned up properly */ }
- 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 */
- }
- void some_function()
- {
- std::vector<a_class> a_instances( 10 );
- // Pass a_instances into these functions by reference rather than by using
- // a global. This is re-entrant and more likely to be thread-safe.
- do_stuff( a_instances );
- do_more_stuff( a_instances );
- // a_instances will be cleaned up automatically here. This also allows you some
- // weak exception safety.
- }
Add Comment
Please, Sign In to add comment