Solved

Can we access class members before class is instantiated?

Posted on 2013-06-11
2
390 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
[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 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 56

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

749 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