Programming Wiki: CppSingleton

Programming Wiki : CppSingleton

WikiHomePage :: Categories :: PageIndex :: RecentChanges :: RecentlyCommented :: Login/Register
Singleton - A class that only lets one object exist for itself by setting all new objects created to point to the original object.
Example from game programming language
symbol_table.h
#ifndef SYMBOL_TABLE_H
#define SYMBOL_TABLE_H

class Symbol_table
{
    public:
    static Symbol_table *instance();
   
    private:
    Symbol_table() {}
};

#endif
 


symbol_table.cpp
#include "symbol_table.h"

//return an instance of the symbol table that is already created
//or return a new one
Symbol_table *Symbol_table::instance()
{
    static Symbol_table symtab;
    return &symtab;
}


Example of creating an instance object of symbol table
Symbol_table *symbol_table = Symbol_table::instance();
    symbol_table->print();      //using the symbol_table object
 

There are no comments on this page. [Add comment]

Powered by Wikka Wakka Wiki 1.1.6.0
Page was generated in 0.0231 seconds