#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();
}
所以,有两个问题:
The
name
objectn
is created in the block insidemain
and 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
name
class:~name() { cout << "destructor called" << endl; }
Does overriding the destructor function, and doing nothing in it (here in
~name()
), prevent the object from being deleted?
编辑:感谢回复,真的很有帮助。但是我可以知道这些踩票的原因吗?我认为这是一个非常好的问题。¯\_(ツ)_/¯