Const,static,mutable keywords within a class

Source: Internet
Author: User
Google Editor is good, you can automatically save, I worked hard for half a day to lose all the words

The keywords in the topic are often involved in the interview, here is a brief introduction to their functions and use of attention.

Const can modify member variables, member functions.
Const modifies a member variable, indicates that the member variable is a const type and cannot be modified after it is defined, and is typically declared within the class, initialized in the constructor's initialization list, because we cannot assign a const variable, so we can initialize it only by initializing the list.
The const modifier member function, in different places, has different meanings. Const precedes the return value to return a Const type object, or a pointer to the const object.  If it is the returned obj, it can assign a value to a const, non-const-type variable, and if it returns a pointer to const OBJ, it can only assign a pointer to const obj. A const is placed at the end of a function to indicate that the function is a const-type function that is read-only for all member functions of a class, can only be read, not assignable, and can only invoke functions of the same const type. Of course, if you need to modify a member variable in a const-type function, C + + provides a way to use the keyword "mutable", which indicates that the member variable is ready to become and can break through the const type function.
Class A
{
Public
A ();
const int Returnobj () {return 10;}
Const int* RETURNPTR () {int b = new int (a); return b;}
void Changev () const {data4=10; Cout<<data4<<endl}
Private
const int DATA1;
int data2;
static int data3;
mutable int data4;
};
int A::d ata3;
A::a (): data1 (value)//value to Initiaize data1
{
//...
}

Static can also modify member variables, member functions. A variable or function that is modified by static to indicate that this variable, function is global, is not part of an object, but belongs to this class. Therefore, you can call only the static type's member variables and member functions in a static member function, because other variables, functions are dependent on the object. Members of the static type are typically declared within a class, defined outside the class. As in the previous example. If you do not assign a value to a member variable of the static type, the default assignment is 0.
The static const modifies a member variable at the same time, indicating that the member variable is a global constant that is declared within the class and defined outside the class.
Class B
{
Public
static const int data = 10;
static const int DATA1;
}

const int B::d ata1 = 20;



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.