what does it mean when private static pointer is assigned "this" ?

Please consider following:

class SomeTask
{
 
  public:
     SomeTask();
    ~SomeTask();
    void initialize();
    static unsigned int _tsk;
     
  private:  
    InterfaceTask *_serviceInterface;
    Protocol * _Protocol;
     
    static SomeTask *_instance;  
 
};

// definition of instance
SomeTask * SomeTask::_instance = NULL;

// I belive following is a constructor
SomeTask::SomeTask()
: _serviceInterface(NULL),
 _Protocol(NULL),
{
  _instance         = this;
}

somewhere in the code, 'new' operator is used to create object of this class and I belive immediatedly above constructor is called.  What is "this" ?  What does it mean to assign "this" to private static pointer '_instance'.


Thank you!
LVL 1
naseeamAsked:
Who is Participating?
 
HooKooDooKuConnect With a Mentor Commented:
In light of evilrix comments, I would add this...

On the surface, it seems like a waste to have a PRIVATE static member variable that only ever contains the value of 'this'.  After all, within the call, you always have access to the 'this' pointer, so why ever store it in any sort of PRIVATE member variable.

The only answer is the fact that a STATIC member variable exists only once regardless of the number of instances of the class you have (i.e. static member variables exist even if you don't have ANY instances of the class).

So I could see trying to utilize a static variables to limit there ever being one instance of a class.  But the given code never attempts to DO anything with this _instance variable... other than to initialize it with the most recent creation of a SomeTask object.  But even worst, nothing is attempted to do with it when SomeTask is destroyed.

In other words, as your programs starts, the global variable SomeTask::_instance is NULL.
When you create an instance of SomeTask, that value changes to the 'this' pointer of the new class.  When the object is then destroyed, the value of SomeTask::_instance is not changed.  It still points to where the previous object used to live (and you'll get access violations if you attempt to use it).  Of course you might say that only an instance of SomeTask could try to access the variable, and since the instance of the object has been destroyed, it doesn't matter.

But on the contrary, static member functions can access static member variables.  So a static function of SomeTask could also access SomeTask::_instance.  In addition, as written, you could have two instances of SomeTask objects.  If you create a 1st instance, then a 2nd instance, then delete the 2nd instance, the 1st instance would have access to SomeTask::_instance, but the variable would be pointing to the now deleted 2nd instance.

So bottom line, to really understand WHY a PRIVATE static member variable containing the value of 'this' was created... we done have a full enough example.  There's basically just too much wrong with what little is shown here.
0
 
evilrixConnect With a Mentor Senior Software Engineer (Avast)Commented:
This isn't a standard C++ thing so I can only guess, but it looks like the idea here is to make a singleton of some kind. If I am right (and I can't really think of any other reason for doing this) it's a pretty poor way to implement a singleton class. Other experts might have an alternative view as so what's going on here so it's probably worth waiting for other considerations.
0
 
HooKooDooKuConnect With a Mentor Commented:
In a c++ class object, 'this' is a pointer to yourself.
In a c++ class object, a static member variable can be described as a global variable that is shared by all instances of the object.

So you basically have a global variable by the name SomeTask::_instance, and the contents of this global variable is a pointer to the most recent instance of the SomeTask class.
0
 
naseeamAuthor Commented:
Very nice detailed accurate explanation.  Very well communicated.
This might be the best response ever.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.