Do I need to clean up non-pointer data members?
c++, destructor
Solution
To be pedantic, it depends on what `MyType` is. If you have `typedef char* MyType` and you allocate memory for `MyType`, and you want that memory to be owned by the object, then yes, you need a destructor.
Otherwise, you don't need to free memory for anything that wasn't allocated with `new` or `malloc`, so no.
Your class shouldn't even have a destructor at all. There's a general consensus that you should only have a destructor when you actually need it. Having a destructor also implies implementing an assignment operator and copy constructor (the rule of three). If you're not managing any memory, it's better to rely on the ones provided by the compiler - i.e. the compiler will generate these three if you don't.
Also, your constructor should look like this:
A::A() : x(42), v(5){
}
Otherwise your members will be initialized and then assigned to, which is wasteful.
Problem
If I don't use `new` to allocate the data members of a class, is there still any clean-up that needs to be done in the destructor? For example, in the following case, will I need to `delete` the vector? Or does it depend on `MyType`? ``` class A { A(); ~A(); MyType x; std::vector<MyType> v; }; A::A() { x = MyType(42); v = std::vector<MyType>(5); } A::~A() { // what goes here? } ```