I don't understand why the destructor was called (the place of this call in the code below )
and if the destructor was called, why the function Print()
could be called?
The code:
class Entity
{
public:
Entity()
{
std::cout << "Ctor Call" << std::endl;
}
~Entity()
{
std::cout << "Destroy Call" << std::endl;
}
void Print()
{
std::cout << "Print me" << std::endl;
}
};
int main()
{
Entity* ent;
{
std::shared_ptr<Entity>sharedPtr = std::make_shared<Entity>();
sharedPtr->Print();
ent = &(*sharedPtr);
} //after this line the destructor was call
ent->Print(); // the print is success
std::cin.get();
}