declare a template type object inside template type class definition

Hi forum,

I am not sure if the subject is clear enough to you. Let me put it into the code snippet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
   This class helps to build the singleton design pattern.
   Here you have full control over construction and deconstruction
   of the object.
*/
   template<class T>
   class Singleton
   {
   public:
      /**
       * Init the actual singleton.
       * Must be called BEFORE the class is used, like this:
       */
      static void init()
      {
	 assert( singletonClass_.get() == NULL);
	 singletonClass_ = new T;
      }


      /**
       * Get Pointer of the actual class
       * @return Pointer of the actual class
       */
      static T* getPtr()
      {
	 assert( singletonClass_.get() != NULL);
	 return singletonClass_.release();
      }

      /**
       * Get reference of the actual class
       * @return reference of the actual class
       */
      static T& getRef()
      {
      	 assert( singletonClass_.get() != NULL );
      	 return *singletonClass_;
      }

      /**
       * Has the actual class been inited?
       */
      static bool isInited()
      {
	 return (singletonClass_.valid());
      }

   private:
      static osg::ref_ptr<T> singletonClass_;
   };

/// init static pointers with 0
   template<class T>
   osg::ref_ptr<T> Singleton<T>::singletonClass_ = 0;




I am getting error at the assertion points when i call to the class as follows:

 
osgOpenCL::Context *cxt = osgOpenCL::Singleton<osgOpenCL::Context>::getPtr();



I tried commenting assertion statements and then the debugger just exits at the point where getPtr() is called .


Any idea to get around this issue?


Regards
Sajjadul
closed account (Dy7SLyTq)
whats the error?
I am getting the following segmentation fault:

1
2
3
4
5
6
7
The inferior stopped because it received a signal from the Operating System.

Signal name : 
SIGABRT
Signal meaning : 
Aborted


At the assertion point of getPtr() function
closed account (Dy7SLyTq)
well it looks like you havent given it a value besides null
Before accessing the ptr i am initializing as follows:

 
osgOpenCL::Singleton<osgOpenCL::Context>::init();


Anything else ?
closed account (Dy7SLyTq)
its still going to be null. you gave it an initial value of zero, and then ran getPtr. its still null
Basically i am trying to implement singleton pattern using the template and smart pointer .

Any reference to get it right?
Is there a specific reason why pointers are involved in this singleton? (normally, you would just return a function-scope static by reference).
Topic archived. No new replies allowed.