Inside The C++ Object Model reading notes (two) -- static member variables and t

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

  About the C++ object model, the former reading notes related to the static member variables, later found to think about or have a lot of, the following one by one.

  First of all, the class of static member is not included in the calculation of the size, type, for example, what is the reason?

class X{
public:
   int data;  
   static int num;
};

sizeof(X) = 4;

  Most people should know, static member is equivalent to global variables, and global variables are stored in static area, so the class or object is separate from, therefore in the calculation of sizeof (X) is not included in the calculation. Since the static member is a global variable, then no matter how many objects programs, static member exists only one instance, even if the class does not have any instance, the static member can also already exists.

  Second, to static member access class declaration, light is not enough, but on the num statement above class X, if you instantiate directly in other functions in one object, and this object to access the static member num, then the compiler will give you a sentence similar to “ fatal error LNK1120: 1 unresolved external command ”. Why? Because the class inside the static int num is simply a statement not defined! If you want to access a variable, it must be defined! While the definition must be outside classes (no other function, with the definition of global variables.). Here can be directly in the class X external direct definition: int X:: num = 10; the definition can directly access the.

  When it comes to the use of static member variables will can't help to think the singleton pattern, the following will introduce place an order mode.

  The singleton pattern (Singleton) is probably the most widely used design pattern, the aim is to ensure for a class, can only instantiate an object of this type, and to provide a global point of access to it. Many situations can match the singleton pattern, such as a system should only have a file system and a window manager, a program is only allowed to produce an interactive window. A common approach is the only instance will create operation hidden in a kind of operation, which ensures that only one instance is created. This operation can save only access instance variables, and assure the instance is not created, create an instance of. The following gives an implementation:

 1 class Singleton{
 2 public:
 3     static Singleton* GetInstance();
 4     int getVar();
 5     void setVar(int);
 6     virtual ~Singleton();
 7 private:
 8     static Singleton* m_instance;
 9     int var;
10 protected:
11     Singleton();
12 };
13 
14 Singleton *Singleton::m_instance=0;
15 
16 Singleton::Singleton()
17 {
18     this->var = 100;
19     cout<<"Singleton constructor."<<endl;
20 }
21 
22 Singleton::~Singleton()
23 {
24     cout<<"Singleton Destructor."<<endl;
25 }
26 
27 Singleton* Singleton::GetInstance()
28 {
29     if(m_instance==NULL)
30         m_instance = new Singleton();
31     return m_instance;
32 }
33 
34 int Singleton::getVar()
35 {
36     return this->var;
37 }
38 
39 void Singleton::setVar(int var)
40 {
41     this->var = var;
42 }

  Can test the getVar in the main function () and setVar () operation, to see whether the operation is the same instance.

  

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Morton at November 12, 2013 - 8:40 PM