The difference between a regular data member and a static data member in a C + + class _c language

Source: Internet
Author: User
Tags constant

Just beginning to learn C + + class and object parts, the class of constant data members and static data members of the concept and usage is often confused, so today to tidy up, by the way, today is my birthday, I wish my birthday happy, hehe.

Regular data members
A regular data member is a data member defined in a class that cannot modify its value, similar to a variable that we have learned before, and has its own address, but once the initial value is assigned, it can no longer be modified.

Applies to variables defined in a class that do not want to be modified after they are initialized.

To define a method:

Copy Code code as follows:

Const type name variable name;

(1) in the class regular data members can only be initialized through the constructor's parameter initialization table.

(2) A regular data member is part of an instantiated object and can be accessed using the this pointer.

Copy Code code as follows:

#include <iostream>
using namespace Std;
Class Happy_birthday
{
Public
Happy_birthday (char * na): Name (NA) {}
void Print ()
{
cout<<name<< "Happy birthday!" <<endl;
}
Private
const char * name;
};
int main ()
{
Happy_birthday Qianshou ("Zhao Zhe");
Qianshou. Print ();
return 0;
}

Run Result:



Static data members
In fact, regular data members and static data members are completely different, but at the beginning of learning is easy to confuse. Through the above explanation, we know that the constant data member is similar to the constant variable, is a variable that can not be changed once the value is assigned.

Their biggest difference is that static data members can be modified, and can be modified by any object, modified values, can be shared by all objects.

A static data member belongs to a class rather than to an object, which is common to all objects defined by the class. The object defined by the class can reference the static member, and the values are the same.

The storage space of a static data member is different from the normal data member, and it is not part of any object of the class, is stored independently of the object, and therefore cannot be accessed through the object's this pointer.

Also, static data members cannot be initialized with the parameter initialization table because it is simple because the initialization table is made using the this pointer when defining the object, so it is not possible.

How static data members are defined:

Copy Code code as follows:

static int num;

Access mode:

can be accessed directly through the class name:

Copy Code code as follows:

Test::s_num;

You can also access by object name:
Copy Code code as follows:

One.s_num;

However, in order to differentiate from other member variables, the class name is generally used for access because static data members are not part of the object and are not misleading.
Copy Code code as follows:

#include <iostream>
using namespace Std;
Class Test
{
Public
Test (int n): C_num (n) {};//can only assign an initial value to a regular data member with an initialization table
void Show ()
{
cout<< "C_num:" <<this->c_num<<endl;
cout<< "S_num:" <<s_num<<endl;
}
void change (int n)
{
S_num=n;
}
static int s_num;
Private
const int C_num;
};
int test::s_num=100;//The class body to assign an initial value to the static data member
int main ()
{
Test one (10);
One.show ();
One.change (10000)//Changing the value of the static data member
cout<< "one changeed:" <<one.s_num<<endl; Indirect access to static data members using object name one
cout<< "Test changeed:" <<test::s_num<<endl;//accessing static data members directly using the class name
Test two (20);
cout<< "two changeed:" <<two.s_num<<endl; Indirect access to static data members using object name two
Two.change (99);
cout<< "Test changeed:" <<test::s_num<<endl;//accessing static data members directly using the class name
return 0;
}

Run Result:

Analysis: We can use the class name directly to access the static data member variables in the program, it directly shows that it does not belong to any one object of this feature. We only assign values to C_num when we define object two, but we can also output the output using Two.s_num, which means that the data member can indeed be referenced by all objects, and that the value is the same, because it does not belong to any one object.

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.