Solved

Can we access class members before class is instantiated?

Posted on 2013-06-11
2
378 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 51

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

747 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

10 Experts available now in Live!

Get 1:1 Help Now