C + + class static member and class static member function detailed __jquery

Source: Internet
Author: User
Tags class definition function definition
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 that member of the other object
will also change. To avoid confusion, it is not necessary to assign a value in the class body
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
class, but only one instance of a static data member exists, regardless of how many class objects are defined. The static method is related to the class,
is a behavior of a class, not associated with an instance object of that 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. Also cannot be in the constructor of a class
, because static data members are shared by individual objects of the class, otherwise static data members are reinitialized each time a class object 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
Will change. 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, this property is common to the entire class,
Does not belong to any one specific object. So static members cannot initialize within the class, 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.




The code is 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:


The code is 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. So that when the object is defined it cannot be started by the constructor
Initiation.
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 access static data and function members of the class directly, while accessing non-static data members must get an object name by passing arguments.
It is then accessed through the object name.
The code is 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 (->) call a static member function for an object of a class or a pointer to a class object, when all of the same class
When an object uses a quantity, you can use a static data member variable for this shared amount, which takes the same value for all objects of the same class. Static into
A member variable can only be called by a static member function. Static member functions are also shared by all objects in the same class. Only static member variables and static member functions can be invoked.

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.