+ 2
Use of destructors cpp
I was instantiating a class, letting the constructor configure my object (intermediate game board) and If it did not conform to a validity check (one player already had won) I wanted to destruct or delete it and reinstantiate. I get an error when I try to reinstantiate the new object with the same name. 2 questions: -How to do this avoiding the error (even if bad idea) -Why is it a bad idea? -Despite the many challenge questions on destructors, it appears one should not use them explicitly. When does it make sense to call a destructor?
5 odpowiedzi
+ 5
There is nothing wrong with it, as long as you do it properly like:
while( true )
{
ttt game;
// do your game
// break to quit, or set some flag to use in the while
}
or:
ttt game;
while( true )
{
// do your game
// break ...
game.reset();
}
// possibly use ttt after the loop
It just depends on how expensive it is to instantiate ttt every time vs only once.
If it's a small object that really doesn't do much nor is called often, it really doesn't matter.
But I've had objects that are many kbs in size, you probably want to avoid reconstructing it every time.
But not like:
ttt* game = new ttt;
while( true )
{
// do game
// break ...
delete ttt;
ttt = new ttt;
}
I mean, it works, but to use it just to reset the game to a valid starting state is pushing it a little.
Not to mention it is slower because of multiple new calls, but not that it matters since it's not called every millisecond.
There are many ways of doing stuff, you decide which make more sense. :)
+ 3
You should never call destructor, it is called when current scope ends. If you want to control how long the variable lives use pointers.
+ 3
Just implement a "clear" or "reset" or something for your class, don't go abuse the destructor for this.
There is 1 valid situation to manually call the destructor and that's with placement new but this is a c++ feature you shouldn't touch unless you're an expert.
It's usually used to implement custom (pool) allocators.
It goes something allong the lines of:
char* ptr = new char[size];
Object* o = new(ptr) Object;
// use o
o->~Object();
delete[] ptr;
Placement new uses the memory of another variable to allocate to.
But now you have to deal with stuff like memory alignment yourself.
+ 1
lpang, this is the code. If the board made by the constructor shows a finished game, I wanted to destruct game and instantiate a new game but I get an error. I know I could handle this from the constructor but I want to know how it can be done (and if there is a way that would be acceptable). Thank you!
https://code.sololearn.com/cvc5bNogA89Y/?ref=app
0
Thank you very much, Dennis, Jakub Stasiak . I was trying to instantiate game like you write for Object. After destroying it the compiler would complain if I instantiated a new game (same name, compiler error that I was repeating game, maybe wrong grammar somewhere). I will try again.
I am also trying to understand why it would be abusing. what are the downs or risks of destroying an object to reinstantiate?