Neal Richter's bits of Tue, 26 Mar 2002 translated to: >Hey all, > > So here's a C++ question for you: > >/* file1.c */ >//global to file 1 >static HtConfiguration *config = NULL; > >/* function1 */ >config = HtConfiguration::config(); >------------------------- >/* file 2 */ >/* function2 */ >HtConfiguration *config = HtConfiguration::config(); > > >These declarations are in two separate files. When the functions are >called, both 'config' variables point to the SAME spot in global memory >where a HtConfiguration object lives.
I believe this class is intended as a singleton. Both pointers end up pointing to the same instance of an HtConfiguration object that is in fact a static member of the HtConfiguration class. The intent of such a design is that there only ever be one instance of the class. >If I change both declarations to > >config = new HtConfiguration(); > >then the variables point to two different objects... (as is expected). As a singleton, the class is not intended to be used in this way. If the static _config member is NULL when config() is called, an instance is created. Otherwise the existing instance is returned. >I've got Stroustrup's C++ book and I've looked up the scoping rules that >govern the '::' instantiation usage... kinda dense. Anyone have a better >reference to what exactly is happening here? (other than the obvious -- >the object appears to be locally allocated but is really global in scope) It is allocated on the heap. For the most part, the config() method is just serving as an accessor. >Any other interesting notes on this kind of usage? A singleton is a fairly common design "pattern". A google search on 'singleton pattern' should provide a lot of useful links. >I also noticed that a call to config->~HtConfiguration() doesn't do much >to delete the object's contents.. there is no defined destructor in >Configuration.cc, just '{}' in the header file. Generally you don't want to destroy the instance since no one really owns it. Jim _______________________________________________ htdig-dev mailing list [EMAIL PROTECTED] https://lists.sourceforge.net/lists/listinfo/htdig-dev