Solved

Can we access class members before class is instantiated?

Posted on 2013-06-11
2
391 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 57

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

Technology Partners: 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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

737 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