Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Singleton
Description: The classic singleton design pattern.Keywords: ecl singleton
Tutorial Level: INTERMEDIATE
Overview
Singletons are a means of ensuring a single, unique copy of an object can ever exist. There are various ways of implementing singletons, the wikipedia has a surprisingly good article on singletons and provides a snippet that utilises the curiously recurring template pattern which is used as the derivation for this form of the singleton.
Compiling & Linking
Include the following at the top of any translation unit that:
Usage
Inheriting
This singleton is intended to be inherited by a deriving class - this saves the work of actually reinventing the singleton mechanisms whenever you wish to use a singleton.
Calling
If you use a macro as shown below, the instance can be conveniently and readably accessed,