How can I use a non-trivial constructor with as little code duplication as possible?

From: William Payne (mikas493_no_spam_at_student.liu.se)
Date: 10/11/04


Date: Mon, 11 Oct 2004 23:22:04 +0200

Hello, consider the following two classes (parent and child):

#ifndef SINGLETON_HPP
#define SINGLETON_HPP

#include <cstddef> /* NULL */

template <typename T>
class Singleton
{
public:
    static T* get_instance()
    {
       if(!t)
       {
          t = new T;
       }

       return t;
    }

private:
    Singleton()
    {
       ; /* Never reached. */
    }

    static T* t;
};

class int_singleton : public Singleton<int>
{
};

/* As with member functions of class templates, the definition must *
  * be in the class header. When compilers properly support the export *
  * keyword, definitions of member functions and static data members *
  * can be moved to an implementation (.cpp) file. */
template <typename T>
T* Singleton<T>::t = NULL;

#endif /* #ifndef SINGLETON_HPP */

The int_singleton class is just an example, I will be using other
(user-defined classes) types in the "real application" (if I can make it
work).

If you look at Singleton::get_instance(), you see that when !t is true
it creates a new object of type T using the constructor that takes no
arguments. However, I need to use constructors that take several
arguments. How should I accomplish this so I get as little code
duplication as possible? I have several rather complex classes in my
program that, and there must be only one instance of each class at all
times, and I would like to use the base class Singleton as much as
possible. Maybe get_instance() should call a virtual create_object()
function when !t is true, a function I redefine in my subclasses?

Comments please, how should I solve this?

/ WP



Relevant Pages

  • Re: OT: C++ Template Functions
    ... Heiko Wundram wrote: ... template <typename T, typename U> ... It is not a problem outside a class, the problem comes when you want to do this inside a class as you also would define a class with an - in theory - unknown and unlimited number of member functions. ...
    (freebsd-hackers)
  • ADDENDA: Re: An exponentiation function for int?
    ... This is for the user to call instead of the member functions: ... template <size_t Exponent_S, typename T> ... It has the advantage of deducing template parameters. ... then our deductions constitute mathematics. ...
    (comp.lang.cpp)
  • Re: template member functions of a templated class
    ... I want a function which takes a range of iterators ... > able to do this with one, or at most two, templated member functions. ... C is not a template. ... See a good book on a better explanation of what InputIterator ...
    (comp.lang.cpp)
  • template member functions of a templated class
    ... (which dereference to type T, or in some cases to type T*), and adds all ... able to do this with one, or at most two, templated member functions. ... C is not a template. ... I looked up the STL vector constructor which takes a range of iterators, ...
    (comp.lang.cpp)
  • Re: How to make obj of temp in temp ???
    ... Then you need to fix the typo in the variable initialization ... template< class T> ... member functions if they existed, unless of course if they are static ... But maybe I just cannot see a good reason, does anyone else see a benefit to ...
    (microsoft.public.vc.language)