Solved

Can we access class members before class is instantiated?

Posted on 2013-06-11
2
383 Views
Last Modified: 2013-06-11
class AlarmTask
{
  public:
    AlarmTask();
    virtual ~AlarmTask();
    ...
    ...
       
    static AlarmTask * getInstance()
    {
      if(_instance == NULL)
      {
        _instance = new AlarmTask();
      }
      return _instance;
    }
   
  private:
   ...
    static  AlarmTask* _instance;
};


// A call to static member of class from init function
__task void init (void)
{
      ...
      // Do all the power up intialization       
      _usb = new UsbSocket();
       pdpTask = new PdpTask(_usb);
      // Get the clock driver constructed.
      AlarmTask::getInstance();
       ...


How can we call class member getInstance() when AlarmTask class hasn't been instantiated yet?
0
Comment
Question by:naseeam
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 425 total points
ID: 39239194
That works because both 'getInstance()' and '_instance' are static class members. Static methods like 'getInstance()' don't recceive a hidden 'this' pointer and therefore are not associated with a particular instance of the class. And you will notice that '_instance' is instantiated (no pun intended) like

AlarmTask* AlarmTask::_instance = NULL;

Open in new window


somewhere else in that code, thus providing storage space for that static variable outside of any instance of 'AlarmTask'. Read more at http://www.learncpp.com/cpp-tutorial/811-static-member-variables/
0
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 75 total points
ID: 39239219
In this case the AlarmTask class is a singleton which means that only one instance of the object should ever be created.

You can create multiple instances with the new operator but in terms of how it has been designed the static function getInstance is static precisely for the purpose of either instantiating an instance of AlarmTask if one has not been created or returning the instantiated object.

The static AlarmTask * _instance - which is declared at the class level but could also have been declared inside getInstance - is a persistant (instance independant - think global) variable that is used to store the instantiated instance of AlarmTask.

Read here for more info on Singleton classes http://en.wikipedia.org/wiki/Singleton_pattern
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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 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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 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

16 Experts available now in Live!

Get 1:1 Help Now