Static object of C + + learning Note

Source: Internet
Author: User
Tags prev

This article is reproduced from http://www.cnblogs.com/morewindows/archive/2011/08/26/2154198.html, thanks to bloggers.

In C + +, static members are the entire class rather than an object, and static data members are actually global variables in the class domain. 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.

static member definitions or declarations are added with a keyword static. A static member can use the scope operator to use the < class name >::< static member name >.

Several examples are used to summarize static member variables and static member function usage rules.

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:

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

Static members can be called in non-static member functions. 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.

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 (); Static member functions

Private

Char M_name[max_name_size];

Student *next;

Student *prev;

Static Student *m_head; Static member variable, list header

};

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; Head insertion, before the head node becomes the subsequent node of the insertion node,

This->prev = NULL; The predecessor node of the current insertion node should be null

if (m_head! = NULL)

{

M_head->prev = this; If the previously linked list is not empty, a new node is now inserted in the head, then the predecessor node of the previous head node (NULL) should be set to the currently inserted node.

}

M_head = this; Update the head node.

}

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; Static member variables are used before initialization

void Main ()

{

Student Studenta ("AAA");

Student studentb ("BBB");

Student STUDENTC ("CCC");

Student studentd ("DDD");

Student Student ("morewindows");

Student::P rintfallstudents ();

}

The program will output:

Morewindows

Ddd

Ccc

Bbb

Aaa

Static object of C + + learning Note

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.