I have the following class which I save in a file in binary mode:
class lol
{
public:
~lol() {
std::cout << "destucted" << std::endl;
system("pause");
}
int age;
std::string name;
};
Now in my main I have this:
int main()
{
{
lol kar;
kar.age = 17;
kar.name = "Blabla";
ytxt("class").Write(std::string((char*)&kar, sizeof(kar))); //Saves the class in binary mode
}
lol * asd;
{
std::string stClass = ytxt("class").Read();//reads the file in binary
asd = (lol*)new char[stClass.length()];//asd is now allocated with new
memcpy_s(asd, stClass.length(), stClass.c_str(), stClass.length());//copy the content of the file into the class
}
std::cout << "Name: " << asd->name << std::endl;//output
std::cout << "Age: " << asd->age << std::endl;
asd->age = 79;
std::cout << "Age: " << asd->age << std::endl;
delete asd;//I cannot delete it as lol
//delete (char*)asd;//works but lol destructor is not called
system("pause");
}
The content of the asd
pointer is being printed to the console, the pointer is deleted and the destructor is called. At the end the exception is thrown.
When I do not delete the asd
pointer everything is working fine, but the destructor of the asd
pointer is not called. I know that the pointer is allocated as new char
and I have to delete it as a new char
, but is there another way where the delete operation calls the destructor of lol
?