C + + class static member and class static member function detailed _c language

Source: Internet
Author: User
Tags class definition function definition

When a data member of a class is declared static, the static data member can only be defined once, and is shared by all objects of the same kind. Each object has a copy of every regular data member in the class, but only one instance of the static data member exists, regardless of how many class objects are defined. A static method is related to the class and is a behavior of the class, not related to the instance object of the class.

One of the uses of static data members is to count how many objects actually exist.

Static data members cannot be initialized in a class, but the class definition is simply a blueprint for describing the object in which the initial value is not allowed. The member cannot be initialized in the constructor of the class, because the static data member is shared by the objects of the class, or the static data members are reinitialized each time the object of the class is created.

A static member cannot be assigned in the class body because it is shared by all objects of that class. You assign a value to it in an object, and the member in the other object changes. To avoid confusion, it is not necessary to assign values in the class body.

The value of a static member is the same for all objects. Static members can be initialized, but can only be initialized outside of the class body.

General form:
Data type class Name:: static data member name = initial value
Note: You cannot initialize a static member with a parameter initialization table. The general system defaults initially to 0.

A static member is a shared member of all objects of a class, not a member of an object. It does not occupy storage space in the object, which is common to the entire class and does not belong to any particular object. So static members cannot initialize within classes, such as declaring a student class, where one member is the total number of students, then the variable should be declared as a static variable and the member variable should be set according to the actual requirements.

Copy Code code as follows:

#include "iostream"
using namespace Std;
Class Test
{
Private
int x;
int y;
Public
static int num;
static int Getnum ()
{
x+=5; This line of code is wrong and static member functions cannot invoke non-static data members, which are invoked through objects of the class.
num+=15;
return num;
}
};
int test::num = 10;
int main (void)
{
Test A;
cout<<test::num<<endl; 10
Test::num = 20;
cout<<test::num<<endl; 20
Cout<<test::getnum () <<endl; 35
Cout<<a.getnum () <<endl; 50
System ("pause");
return 0;
}

Through the above example know: x+=5; This line of code is wrong
A static function member must access a non-static data member through an object name.
In addition, static member functions are not required to add the static keyword when implemented outside the class, or they are wrong.
If you implement the static member function above in the body of the class, you cannot add the statically keyword, so write it:
Copy Code code as follows:

int Test::getnum ()
{
.........
}

1. The owner of the static member is the class itself and the object, but more than one object has the same static member. This defines the object as being unable to initialize it through a constructor.

2, static members can not be initialized in the class definition, can only be initialized outside of the class body.

3, static members still follow the public,private,protected access guidelines.

4. A static member function does not have the this pointer, and it cannot return non-static members because the class itself can be invoked in addition to the object calling it.

Static member functions can directly access the static data and function members of the class, while access to non-static data members must be passed by parameter to obtain an object name, which is then accessed through the object name.

Copy Code code as follows:

Class Myclass
{
Private
int a,b,c;
static int Sum; Declaring static data members
Public
Myclass (int a,int b,int c);
void Getsum ();
};
int myclass::sum=0; Define and initialize static data members
Myclass::myclass (int a,int b,int c)
{
this->a=a;
this->b=b;
this->c=c;
Sum+=a+b+c;
}
void Myclass::getsum ()
{
cout << "sum=" <<sum <<endl;
}
int main (void)
{
Myclass Me (10,20,30);
Me. Getsum ();
System ("pause");
return 0;
}

The above example shows that non-static member functions can access static member functions and static data members arbitrarily.
Non-static member functions MyClass (int a,int b,int c) and Getsum () have access to static data member sum.
Static member functions do not have access to non-static member functions and non-static data members.

For static member functions, you can summarize the following points:

A function definition that appears outside the class body cannot specify a keyword static;

Static members can access each other, including static member functions accessing static data members and accessing static member functions;

Non-static member functions can access static member functions and static data members arbitrarily;

Static member functions do not have access to non-static member functions and non-static data members;

Because there is no extra overhead for this pointer, static member functions have a slight increase in speed compared to the global functions of a class;

Calling a static member function, you can use the member access operator (.) and (->) calls a static member function for an object of a class or a pointer to a class object. When all objects of the same class use a quantity, a static data member variable is used for this shared amount, which takes the same value for all objects of the same class. Static member variables can only be called by static member functions. Static member functions are also shared by all objects in the same class. Only static member variables and static member functions can be invoked.

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.