I'm trying to deallocate the memory of an object using destructor. this memory was allocated on the declaration of the object. the code snippet below is a comprehensive version of the program I'm making, but it explains my problem. I know that the problem is coming in deallocation via destructor but I have no clues what might be cause of the exception.
#include <iostream>
using namespace std;
class student
{
private:
char const* firstname;
char const* lastname;
public:
student()
{
firstname = new char[10];
lastname = new char[10];
firstname = "HelloMaria";
lastname = "Something";
}
void print()
{
cout << "Firstname = " << firstname<<endl;
cout << "Last name = " << lastname << endl;
}
~student()
{
cout << "Destructor Called" << endl;
delete[] firstname;
delete[] lastname;
}
};
int main()
{
student obj1;
obj1.print();
system("pause");
return 0;
}
Aucun commentaire:
Enregistrer un commentaire