I'm a c++ beginner and now reading the C++ Primer. I have some problem about the destrucor:
in chapter 13.1.3: "In a destructor, there is nothing akin to the constructor initializer list to control how members are destroyed; the destruction part is implicit. What happens when a member is destroyed depends on the type of the member. Members of class type are destroyed by running the member’s own destructor. The built-in types do not have destructors, so nothing is done to destroy members of built-in type."
So if I have such a class:
class Foo {
~Foo() { delete pi; }
int *pi;
int i;
};
When an instance of Foo is going to be destroyed, according to the above text, the built in type variable int i will not be destroyed? Then how it will exist since the object to which it belongs has been destroyed.
In chapter 13.1.4: "The HasPtr class that we have used in the exercises is a good example (§ 13.1.1,p.499). That class allocates dynamic memory in its constructor. The synthesized destructor will not delete a data member that is a pointer. Therefore, this class needs to define a destructor to free the memory allocated by its constructor."
I understand that the synthesized destructor does not de-allocate the memory that has been allocated by
newand is pointed to by plain pointers, but here in the text, it indicates that the pointer object itself is also not deleted by the synthesized pointer. Still the example of classFoo, does that mean that after an object ofFooclass is destroyed, the pointer pi will not be destroyed ( so becomes an invalid pointer )?