Solved

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

Posted on 2013-06-07
4
476 Views
Last Modified: 2013-06-07
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!
0
Comment
Question by:naseeam
  • 2
4 Comments
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 50 total points
ID: 39229624
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
 
LVL 16

Assisted Solution

by:HooKooDooKu
HooKooDooKu earned 450 total points
ID: 39229627
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
 
LVL 16

Accepted Solution

by:
HooKooDooKu earned 450 total points
ID: 39229681
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
 

Author Closing Comment

by:naseeam
ID: 39229999
Very nice detailed accurate explanation.  Very well communicated.
This might be the best response ever.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now