Const CHAR * Const Foo (Char const * const str) const; what does it mean?

zhaozj2021-02-17  59

Const is generally used to represent constants, and if you don't want to be changed in the program you write, you will be clearly stated as a constant class.

That is, you try to change the value of constant

When you will report an error, you can write this way.

CONST FLOAT PI = 3.1415926;

Can also write this way

Float const pi = 3.1415926;

Their meaning is the same. So if you write this:

Const float const pi = 3.1415926;

It will be repeatedly defined. Duplicate `const '

And there is a different meaning of the pointer const on the left or right side of the pointer, and the const represents the meaning of the pointer left.

It is constant, for example:

Const char * str = "foo";

Or do you write a char const * str = "foo";

It is to indicate that foo can't change, so you have to do it for Const variables, for example:

STRCPY (STR, "ABC");

Note that StRCPY is CHAR * STRCPY (Char * Dest, Const Char * SRC);

Type 1 is a char * is not a constant pointer, so it will be reported when modifying constants, like this:

Cannot Convert Char * 'To `Char *' for argument` 1 'to strcpy (char

*, const char *) '

Of course, this is just the operation of compile, and the rogue programmer can still change the value of the constant, for example

STRCPY ((char *) STR, "ABC");

Or char * strtmp = str; strcpy (strcpy (strtmp, "abc");

But no matter what, write const on writing, easy to find that the programmer makes a lower mistake, such as Strcpy to target the target.

And the source is against.

Now, const is in the right of * on the right, which means that the pointer is not allowed to change, for example:

Char * const str = "foo";

Str = "xixi"; // Haha This is limited by it, it is not possible.

At this time, the compilation will appear, Assignment of Read-Only Variable `str ', see that even the pointer itself cannot be changed.

changed.

Now everyone looks again, I used to mention the question:

Const Char * Const Foo (Char Const * Const Str) Const;

Where the first constant returns a const, you can't change the value, so you can't use it.

STRCPY ("XiXi"), "ABC");

The second constant indicates that the pointer cannot be changed. It is theoretically to use this, foo ("xixi") = "oo"; but functions

The return value itself is Readonly

Therefore, it is not true to write this way, this const can be omitted. The third representation of the Str is a constant, pay attention

I am deliberately putting on the back of char.

It is to distinguish between the position of the function returned to the value, the actual place is the same, and the 4th Const means STR.

The pointer can't mean something.

The last const is C , which can only be used in the method, indicating that this method does not change any object.

value.

All constings here are for effective checks to change some things that should not change, but remember

With: any way does not prevent real rogue programmers, this is just letting you try to resolve the program possible when compiling.

A stream

Rogue programmer.

转载请注明原文地址:https://www.9cbs.com/read-28699.html

New Post(0)