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

x
?
Solved

Can we access class members before class is instantiated?

Posted on 2013-06-11
2
Medium Priority
?
398 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 1700 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 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 300 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
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.

715 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