✅ Give object default values (destroy?)
Hi I have a program which after sign in assignes object user some values. While sign out i need to delete/destroy or set as default those vaules.
I've heard about deconstructor but i dont know how to use them to be honest. Please help me
10 Replies
forget about the deconstructor
either set the whole user to null, which would make more sense, or make a method that resets them
you have to do that manually
if it were a struct tho, you could've done
this = default
okay but earlier in my code im creating new User user
it won't give me an error?
idk I don't have your code
a ihave a login function that creates user object
now i want to create a function that log out
no user can sign in, log out and the again sign in for example
that is business logic, nothing to do with destroying or destructing a user instance
What this would do is when the instance itself is garbage collected, the destructor is run, which would make it completely useless.
so can I do that and it will be completly fine?
and will it overwrite it right?
well, no. 'user' is the same variable name. But apart from that, you have a valid duplicate
its not overwriting anything
The first
User
instance will get GC'd at some point"Garbage collected" or "GC'd" is when an object has no references throughout the entire program. It's inaccessible, gone, discarded. At that point, the destructor might be run, before the memory is freed entirely, but you can't really do anything of value at that point.
anyway, as already stated, destructors are not the right tool here.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.