I've received some C++ code with various structures defined like this:
typedef struct _someStruct_ {
std::string someString;
std::vector<std::string> someVectorOfStrings;
int someOtherStuff;
~_someStruct_()
{
someString.clear();
someVectorOfStrings.clear();
}
} someStruct;
Is the destructor here completely redundant - if the structure were to be destructed by the default destructor, won't any strings, vectors, etc. be destructed anyway?
If I'd written the code, I wouldn't have thought of adding an explicit destructor here - I'd just let the compiler get on with it.
As I understand it, the only time you might need to create your own destructor in a struct is if any of the members of the structures contain pointers to data that might need cleaning up, or if some extra functionality (e.g. for debugging, logging when a structure gets deleted) is needed.
Am I missing something here - is there any reason why strings and vectors have been explicitly cleared in the destructor? My suspicion is that the person who sent me this is really a C programmer (cf. the typedef) who's tried to turn some C code into C++.