Static members of C + + classes deeply parse _c language

Source: Internet
Author: User
Tags prev

In C + +, static members belong to the entire class rather than to an object, and the static member variable stores only one copy for all objects. So you can share it in all the objects. Using static member variables to implement data sharing between multiple objects does not break the principle of hiding, guarantees security, and saves memory.

The definition or declaration of a static member is added to a key static. Static members can be used by double colons, that is, the < class name >::< static member name >.

In C + + class static member variable and static member function is an error-prone place, this article first through a few examples to summarize the static member variables and member functions using rules, and then give an example to deepen the impression. I would like to read this article to give readers a deeper understanding of the static member variables and member functions of the class.

In the first example, static member functions and non-static member functions are invoked through class names

Copy Code code as follows:

Class Point
{
Public
void Init ()
{
}
static void Output ()
{
}
};
void Main ()
{
Point::init ();
Point::output ();
}

Compilation Error: Error C2352: ' point::init ': illegal call to non-static member function
Conclusion 1: Non-static member functions of a class cannot be invoked through the class name.

In the second example, static member functions and non-static member functions are invoked through the object of the class
Change the main () of the previous example to:
Copy Code code as follows:

void Main ()
{
Point pt;
Pt.init ();
Pt.output ();
}

Compiled through.
Conclusion 2: Objects of the class can use static member functions and non-static member functions.

The third example is to use a non-static member of a class in a static member function of a class
Copy Code code as follows:

#include <stdio.h>
Class Point
{
Public
void Init ()
{
}
static void Output ()
{
printf ("%d\n", m_x);
}
Private
int m_x;
};
void Main ()
{
Point pt;
Pt.output ();
}

Compilation error: Error C2597:illegal reference to data member ' point::m_x ' in a static member function
Because static member functions are part of the entire class, space is allocated before the class instantiates the object, and the non-static members of the class have to have memory space after the class instantiates the object, so the call goes wrong, just like not declaring a variable but using it in advance.
Conclusion 3: Non-static members cannot be referenced in static member functions.

The fourth example uses static members of a class in a Non-static member function of a class
Copy Code code as follows:

Class Point
{
Public
void Init ()
{
Output ();
}
static void Output ()
{
}
};
void Main ()
{
Point pt;
Pt.output ();
}

Compiled through.
Conclusion 4: A non-static member function of a class can be invoked with a static member function, but vice versa.

The fifth example uses static member variables of a class
Copy Code code as follows:

#include <stdio.h>
Class Point
{
Public
Point ()
{
m_npointcount++;
}
~point ()
{
m_npointcount--;
}
static void Output ()
{
printf ("%d\n", M_npointcount);
}
Private
static int m_npointcount;
};
void Main ()
{
Point pt;
Pt.output ();
}

Press CTRL+F7 compile no error, press F7 to generate EXE program times link Error
Error lnk2001:unresolved external symbol "Private:static int point::m_npointcount" (? m_npointcount@point@@0ha)

This is because static member variables of a class must be initialized before they can be used.
Add int point::m_npointcount = 0 before the main () function;
Recompile link no error, run program will output 1.
Conclusion 5: Static member variables of classes must be initialized and reused.

Combining the above five examples, the static member variables and member functions of the class are summarized:
One. Non-static members cannot be invoked in static member functions.

Two. Static members can be called in non-static member functions. Because static members belong to the class itself and exist before the object of the class is generated, static members can be invoked in non-static member functions.

Three. Static member variables must be initialized before they can be used (such as int myclass::m_nnumber = 0;), otherwise there will be an error linker.

To give an example of the static member variables and functions of a class to deepen understanding, this example builds a student class, and each student class object will form a two-way list, with a static member variable to record the table header of the doubly linked list, and a static member function to output the doubly linked list.

Copy Code code as follows:

#include <stdio.h>
#include <string.h>
const int max_name_size = 30;

Class Student
{
Public
Student (char *pszname);
~student ();
Public
static void Printfallstudents ();
Private
Char M_name[max_name_size];
Student *next;
Student *prev;
Static Student *m_head;
};

Student::student (char *pszname)
{
strcpy (This->m_name, pszname);

Create a two-way list with new data inserted from the head of the linked list.
This->next = M_head;
This->prev = NULL;
if (M_head!= NULL)
M_head->prev = this;
M_head = this;
}

The student::~student ()//destructor process is the node's detach process
{
if (this = = M_head)//The node is the head node.
{
M_head = this->next;
}
Else
{
This->prev->next = this->next;
This->next->prev = this->prev;
}
}

void Student::P rintfallstudents ()
{
for (Student *p = M_head p!= NULL; p = p->next)
printf ("%s\n", p->m_name);
}

student* student::m_head = NULL;

void Main ()
{
Student Studenta ("AAA");
Student studentb ("BBB");
Student STUDENTC ("CCC");
Student studentd ("DDD");
Student Student ("morewindows");
Student::P rintfallstudents ();
}

The program will output:



Of course in this example can also add a static member variable to indicate the number of secondary students linked list, if the reader is interested, this as a small exercise bar.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.