Rotating: Static members of C + + classes are explained in detail

Source: Internet
Author: User

In C + +, a static member is an entire class rather than an object, and a static member variable stores only one copy for all objects to be shared. So it can be shared across all objects. Using static member variables to implement data sharing between multiple objects does not break the principle of shadowing, guaranteeing security and saving memory.

The definition or declaration of a static member is to add a key static. Static members can use a double colon, which is the < class name >::< static member name >.

In C + +, the static member variables and static member functions of the class is an error-prone place, this article first summarizes the static member variable and the member function usage rule through several examples, and then gives an example to deepen the impression. Hopefully reading this article allows readers to have a deeper understanding of the static member variables and member functions of the class.

The first example, calling static member functions and non-static member functions through the class name

Class Point

{

Public

void Init ()

{

}

static void Output ()

{

}

};

void Main ()

{

Point::init ();

Point::output ();

}

Compilation Error: Error C2352: ' point::init ': illegal call of non-static member function

Conclusion 1: A non-static member function of a class cannot be invoked through the class name.

The second example, calling static member functions and non-static member functions through the object of the class

Change Main () of the example above to:

void Main ()

{

Point pt;

Pt.init ();

Pt.output ();

}

Compiled by.

Conclusion 2: Objects of a class can use static member functions and non-static member functions.

Third example, using a non-static member of a class in a static member function of a class

#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 a static member function belongs to the entire class, space is allocated before the class instantiates the object, and the non-static member of the class must have memory space after the class instantiates the object, so the call goes wrong, just as it is used without declaring a variable.

Conclusion 3: Static member functions cannot refer to non-static members.

Fourth example, using static members of a class in a non-static member function of a class

Class Point

{

Public

void Init ()

{

Output ();

}

static void Output ()

{

}

};

void Main ()

{

Point pt;

Pt.output ();

}

Compiled by.

Conclusion 4: a non-static member function of a class can be called with a static member function, but vice versa.

Fifth example, using static member variables of a class

#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" ([email protected]@@0ha)

This is because static member variables of a class must be initialized before they are used.

Add int point::m_npointcount = 0 before the main () function;

To compile the link without errors, the running program outputs 1.

Conclusion 5: Static member variables of a class must first be initialized and reused.

In combination with the above five examples, the static member variables and member functions of the class are summarized:

One. A non-static member cannot be called in a static member function.

Two. A static member can be called in a non-static member function. Because static members belong to the class itself, they already exist before the object of the class is produced, so static members can be called in non-static member functions.

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

Give an example of the static member variables and functions of the class to deepen the understanding, this example builds a student class, and each student class object will form a doubly linked list with a static member variable that records the table header of the doubly linked list, and a static member function outputs the doubly linked list.

#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 doubly linked list, and the new data is inserted from the list header.

This->next = M_head;

This->prev = NULL;

if (m_head! = NULL)

M_head->prev = this;

M_head = this;

}

Student::~student ()//The process of destruction is the separation process of the node

{

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 represent the number of students in the chain list, if the reader is interested, this as a small exercise.

Reprint please indicate source, original address: http://www.cnblogs.com/morewindows/archive/2011/08/26/2154198.html

Rotating: Static members of C + + classes are explained in detail

Related Article

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.