views:

15

answers:

2

Suppose I have a state in a program that stores the information of a user, and the platform I am on is not stateless (say a C++ or .NET desktop application).

   class UserInfo
    {
      private:
        std::string name_;
        std::string accountID_;
     }

Say the user logs out, in general, what are the best practices for state reinitialization?

  1. Write and Call a Reset() function on UserInfo and set all the variables to blank
  2. Delete UserInfo, create a new instance to store the new user.

Just to clarify, I am only using the UserInfo as an example; this could apply to things like an entire dialog or subsystems.

+1  A: 

Unless there's a major overhead, deleting and recreating is simpler, more straightforward and requires less code (which means fewer bugs). I'd go with it over Reset() in most cases.

Max Shawabkeh
+1  A: 

Using Reset could lead to data not being cleaned out properly, a residue in memory for instance and it implies one singular UserInfo which I would not think as scalable. I would rather create a new instance each time (if there is a constraint on doing so, then ensure thoroughly that the Reset does exactly that).

What do you think?

Hope this helps, Best regards, Tom.

tommieb75

related questions