Is there any good reason to not clear/zero an object's basic member data in the destructor? It seems like this should be considered standard practice for the sake of tidiness or privacy since there are conceivable ways to re-read the data from a deleted object:
#include <iostream>
class Box {
public:
Box(int s) : secret(s), buffer(this) {}
~Box() {}
/*BETTER DESTRUCTOR ~Box() {secret = 0;}*/
void print() {std::cout << secret << std::endl;}
private:
void* buffer;
int secret;
};
int main() {
Box* carton = new Box(8675309);
Box* crate = carton;
delete carton;
crate->print();
}
(Note that buffer
is only necessary because without it, something overwrites secret
before crate->print()
.)
Does it take "too much time" to zero the member data so that it's considered to be not worth it (especially if you have to walk over an array and zero each element)?
Apparently this is not an issue with more complicated data members like STL containers because their destructors clear their data, and also pointers should not be cleared for debugging purposes.