TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: NEIL HELLER
from: WIM VELDHUIS
date: 1997-03-23 22:26:00
subject: STATIC? SAY WHAT?

Neil Heller wrote in a message to Chris Downs:
 NH> Are there any times when the function would need to be
 NH> called prior to its instantiation as an object?  For that
 NH> matter, can you think of a viable necessity for a static
 NH> function?
Yes, for instance in the singleton pattern. The singleton pattern is used to 
make sure only one instance of a certain class exists. This is done by using 
a static member variable and a static accessor function to actually access 
the member. For instance there should be only one Screen instance in your 
program, you could code is as follows :
class Screen {
private:
  static Screen* m_pInstance;
  Screen() {};   // No one is allowed to create, copy or 
                 // destroy a screen
  Screen( const Screen& ) {}
  ~Screen() {};   
public:
  static Screen* Instance();
};
Screen* Screen::m_pInstance = 0;
Screen* Screen::Instance()
{
  if( m_pInstance == 0 ) {
    m_pInstance = new Screen;
  }
  return m_pInstance;
}
There are of course other uses like creator functions which will return an 
instance of the class when called. These functions can then be stored 
somewhere and be used to create an instance of the class. In large programs I 
often end up using a certain class to instantiate members of an inheritance 
tree, which then initialise themselves from a database after they are 
created. The code to create them then looks like :
struct EqListItem {
  int16 list_id;
  int16 eq_type;
  int16 eq_id;
};
EquipmentList::ReadFromDatabase( DB& db )
{
  // this code reads a table from the database and creates (and
  // initialises) all instances which should be in this list.
  Flush(); // remove all old entries
  SQLStatement sql( db );
  EqListItem eli;  // struct containing all fields of the table.
                   // will be used as buffer for db transactions.
  eli.list_id = 10;
  sql.add( "select eq_id, eqtype from equiplist where list_id=", 
           &eli );
  sql.Execute();
  for( sql.First(); sql.Next(); sql.success() ) {
    Equipment* p = EqTypes::Instance()->createByType( eli.eq_type );
    p->ReadFromDatabase(db, eli.eq_id );
    Add( p );
  }
}
This is only a line-out of the method used, EqTypes maintains an association 
between the type constant and the creator function and uses this association 
to create an instance. If I later add a new type, this code needs not to be 
changed since it never actually uses the type information itself. The system 
could even be dynamic, you can change alter the types known by EqTypes 
instance on run-time if necessary.
I hope this makes some uses clear.
mvg/wr
  
--- timEd/2 1.01.g3+
---------------
* Origin: LightHouse BBS ==> I am a H.U.G.O. Member ! (2:285/324.3)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.