Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-06-07
4
Medium Priority
?
494 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 200 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 1800 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 1800 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
 
LVL 1

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

661 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