[Cs3] singletons and inhertiance

Mikhail Nesterenko mikhail at cs.kent.edu
Mon Jul 28 20:12:45 EDT 2014


CS3 students,

there was a discussion during the lab about inheriting singletons. It
is relatively simple to create a regular class and then inherit it
such that the derived class is a singleton. That is, the functionality
of enforcing the uniqueness is in the derived class.

However, what if the objective is the opposite: somehow encapsulate
the uniqueness-enforcing functionality in the in one class and then
have several classes use it so that it is not duplicated?

     That is, we need to have class A and class B that are both
     different but there can be at most one instance of A and at most one
     instance of B.

Inheriting from a singleton class is not simple. Both Classic and
Meyers implementations of Singleton use static methods that are not
inheritable.

The most straightforward way of abstracting Singleton functionality is
through templates. Here is an example:

    http://www.cs.kent.edu/~mikhail/classes/cs3/Examples/Templates/DesignPatterns/singleton.cpp

Thanks,
-- 
Mikhail


More information about the cs3 mailing list