I have a utility that reads and writes objects to disk. I have an object Params that gets stored to disk via the utility. Params is a singleton that gets created when the user first starts the program, and initializes the users data. So in the private constructor to Params, I first check whether the file exists on disk, and if it does, I want to use it to create my Params object. I though maybe I could just use "this", but the compiler is complaining. How best to do this?
[This message has been edited by eric moon (edited January 12, 2001).]
<BLOCKQUOTE><font size="1" face="Verdana, Arial">quote:</font><HR>"Those who cast the votes decide nothing. Those who count the<BR>votes decide<BR>everything." <BR> -Joseph Stalin<HR></BLOCKQUOTE>
Originally posted by eric moon: I have a utility that reads and writes objects to disk. I have an object Params that gets stored to disk via the utility. Params is a singleton that gets created when the user first starts the program, and initializes the users data. So in the private constructor to Params, I first check whether the file exists on disk, and if it does, I want to use it to create my Params object. I though maybe I could just use "this", but the compiler is complaining. How best to do this?
As you found out you can't assign a new object to "this". Since you are already using a factory method for the singleton pattern, why not put the creation detail, i.e. the choice between deserializing from disk, or creation from scratch, in that factory?
HTH - Peter
eric moon
Ranch Hand
Joined: Nov 26, 2000
Posts: 133
posted
0
Oh, that's how I do it?? My class actually contains a single static instance of itself?? Trippy freak-out weird! I'll try it. Thanks! e