C + + const modifier variables and Cosmetic functions introduction to _c language

Source: Internet
Author: User
Tags constant gety modifier

const modifier Variable

One of the most common interview questions about Const is this: what is the difference between Char *const and const char*, and everyone knows that the const modifier represents a constant, that a const-modified variable, once initialized, cannot be changed, and the two types represent a pointer that is immutable. A representative pointer points to the content is not variable, but the specific which corresponds to which, many people have been confused.

There is a rule where the Const modifier is all of the data types in front of it, and if the const is at the front, swap it with the first Data class row after it. For example, the above const char* Exchange is char const *, which makes it clear that Char *const The const modifier in P is char * (note, here we take char and * as a type, where the const modifier is a combination of char and *, which is the string pointer, and is a pointer type, so the pointer p cannot be changed, for example, the following code will give an error.

Copy Code code as follows:

Char str1[]= "STR1";
Char str2[]= "STR2";
Char *const p = str1;
p = str2;

At this point P is a pointer constant, and it cannot be pointed to anywhere else, but the content it points to can be changed, for example, the following operation is allowed
Copy Code code as follows:

Char str1[]= "STR1";
Char *const p = str1;
P[0] = ' X ';
printf ("%s", str1);

That's when str1 's value becomes "Xtr1."
Let's take a look at the const char *p, which, according to the rules mentioned earlier, turns the const and one of its subsequent types into Char const *P (This is also permissible, but it is customary to write the const at the front), when the const modifier is char, That is, the character content that p points to cannot be changed. To convert all char *const p of the above two examples to const char *P, the result is the opposite, the first one can be compiled and the second will be an error.

Other times it's a good distinction, such as const int, const string, and so on, in short, what the const modifies is what type, the variable of this type cannot be changed.

Const modifier function

Let's look at a function like this

Copy Code code as follows:

const char * func (const char *str) const;

Such a function is more exaggerated, there are three const, we from left to right to one by one description:

1. The first const modifier is the return value, which has already been mentioned, where the const modifier is char, which means that the contents of the returned value cannot be changed
2, the second const and the first is the same, this is used more, as a function parameter, which means that the parameter cannot be altered in the body of the function (the argument passed in is not required to be a const type), in order to prevent the function from doing some unexpected action on the argument, just imagine, When you call a function, you pass in a variable is "Hello world!", after the function is changed to "Fuck the world!", this is really not tolerated, so we design the function, if the incoming parameters are used only as read, It is best to set the parameter to a const type. Many companies look at the details when they write code, and you notice that the details do not necessarily mean you are good, but if you do not pay attention to it will definitely reduce the points.
3, and then the third const, according to our first rule, the Const modifier is all of the data types that precede it, where all the data types are grouped together as a function, and this type is generally present in the class member function, which, of course, does not mean that the function is immutable, It represents the time when this function cannot change the member variables of the class, whether public or private.

Here are a few examples to illustrate the third situation, to see such a simple procedure

Copy Code code as follows:

#include <stdio.h>

Class A
{
Public
A (): X (0), y (0) {}
void func (const int p)
{
x = P;
y = p;
}
int GetY ()
{
return y;
}
int x;
Private
int y;
};

int main (int argc, char* argv[])
{
A A;
printf ("x:%d y:%d\n", a.x, A.gety ());
A.func (2);
printf ("x:%d y:%d\n", a.x, A.gety ());
return 0;
}

This code can be compiled directly, and the result of the operation is
Copy Code code as follows:

x:0 y:0
X:2 Y:2

We modified the void func (const int p) to void func (const int p) and then compiled it directly, and the two lines of error are
Copy Code code as follows:

x = P;
y = p;

That is, the const type function is illegal to try to modify the member variable of the class, but there is one case where we make a little modification on the basis of the above modification, change int x to mutable int x, and change int y to mutable int y, and then the program works again. That is, if the member variable is of type mutable, it can be modified in any scenario.

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.