I have a template class with an instantiation parameter. I have another class that has a member parameter of that class.
The follow does not compile because it says instantiation method is deleted...
class MyClass {
public:
MyTClass<AClass> tclass;
}
The following works but I am not sure if it is the correct way...
class MyClass {
public:
MyTClass<AClass> tclass = MyTClass<AClass>(1234);
}
It seems the member paramter is instantiated but is it tidied up on owner class destruction?
I wish to avoid new and delete if I can to keep my code tidy so I am thinking I wish to take advantance of member parameter instantiation on class instantiation. As I have added explicit instantiation, do I need to fall back to new and delete to ensure it is destructed?
No, you do not need to do anything extra here. The destructor of MyClass
invokes the destructor of all members, regardless of how they were originally constructed.
(This is true even if the member is a pointer. But the destructor of a pointer does nothing, which is why you'd need a delete
call to destruct and free the thing it points to.)