To check this, I have run this small test code.
#include <iostream>
using namespace std;
class name {
public:
int data;
name(int data) {
this->data = data;
}
void printData() {
cout << data << endl;
}
};
int main() {
name* namePointer;
{
name n(5);
namePointer = &n;
}
namePointer->printData();
}
So, two questions:
The
nameobjectnis created in the block insidemainand its pointer is saved, to make sure its destructor is called when we get out of the block. But the pointer points to the same object and its member functions are still accessible. Doesn't this mean that the object isn't deleted yet?Say I add this to my
nameclass:~name() { cout << "destructor called" << endl; }Does overriding the destructor function, and doing nothing in it (here in
~name()), prevent the object from being deleted?
Edit: Thanks for the responses, it really helped. But May I know the reason for these down votes, I think this is a really good question. ¯_(ツ)_/¯