How do I manually delete an instance of a class?
Example:
#include <iostream>
#include <cstring>
class Cheese {
private:
string brand;
float cost;
public:
Cheese(); // Default constructor
Cheese(string brand, float cost); // Parametrized constructor
Cheese(const Cheese & rhs); // Copy construtor
~Cheese(); // Destructor
// etc... other useful stuff follows
}
int main() {
Cheese cheddar("Cabot Clothbound", 8.99);
Cheese swiss("Jarlsberg", 4.99);
whack swiss;
// fairly certain that "whack" is not a keyword,
// but I am trying to make a point. Trash this instance!
Cheese swiss("Gruyère",5.99);
// re-instantiate swiss
cout << "\n\n";
return 0;
}
Use the del keyword to delete class instance in Python. It's delete references to the instance, and once they are all gone, the object is reclaimed.
When delete is used to deallocate memory for a C++ class object, the object's destructor is called before the object's memory is deallocated (if the object has a destructor). If the operand to the delete operator is a modifiable l-value, its value is undefined after the object is deleted.
Delete a User-Defined Class Object in C# by Assigning null Value to It. A class object is a reference variable that points to the memory location of that class. We can delete the object by assigning the null value to it. It means that the object currently contains no reference to any memory location.
Without knowing the use-case or the actual problem you want to solve (please read about the XY problem, your question is a good example of it) the simplest way is just reassigning:
Cheese swiss("Jarlsberg", 4.99);
...
swiss = Cheese("Gruyère",5.99);
That might of course require you to implement an assignment operator, but following the rules of three or five you should do that anyway (but the assignment operator is not needed if you follow the rule of zero).
You could also use pointers, if you explicitly want to destroy the current swiss
object:
Cheese* swiss = new Cheese("Jarlsberg", 4.99);
...
delete swiss;
swiss = new Cheese("Gruyère",5.99);
But pointers is a can of worms that you should avoid, and don't really need much in modern C++. But pointers (or references) are needed if you want polymorphism. Then you could have a pointer to the base class pointing to the actual instance, and things like virtual functions will work as expected.
Also, and depending on your situation which we still know nothing about, you could of course use scoping:
Cheese swiss("Jarlsberg", 4.99);
...
{
Cheese swiss("Gruyère",5.99);
// In here the swiss cheese is a Gruyère
...
}
// Out here the swiss cheese is a Jarlsberg
Though shadowing variable names like this works, it's a bad habit that you should avoid as it adds confusion for readers of the code. On the other hand, even when using scopes nothing stops you from using any (valid) variable name you want, so you could name the outer scope instance jarlsberg
and the inner scope instance gruyere
, the gruyere
object would then be destructed at the end of the scope just like any other nested-scope variable would be destructed and "disappear".
One can use scoping to allow you to define another instance of a class.
Cheese swiss("Toe", 3.14)
{
Cheese swiss("Ear", 15.9);
}
As a general rule locally declared instances will destroy themselves when they go out of scope.
If you really feed the need to destroy cheese, then you need to dynamically allocate it instead.
Cheese *swiss = new Cheese("toe", 3);
// do something with swiss.
delete swiss; // throw it away.
swiss = new Cheese("Ear", 7);
// do something with swiss.
delete swiss; // throw it away.
Dynamically allocated memory must always be manually deleted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With