| |

VerySource

 Forgot password?
 Register
Search
View: 3087|Reply: 18

Question about const?

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-16 16:20:01
| Show all posts |Read mode
In C ++ this statement extern const int&ri; is it a declaration or a definition, why? It is not necessary to initialize it when it is referenced, and bind it to an object?
Reply

Use magic Report

0

Threads

41

Posts

28.00

Credits

Newbie

Rank: 1

Credits
28.00

 China

Post time: 2020-1-21 11:36:01
| Show all posts
Well,
If it is const, it must be assigned at the time of definition, otherwise you have no chance to assign
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-21 12:18:01
| Show all posts
statement.
Must be initialized when defined.

Note: Because it is used in external files, it is also necessary to use extern to make it an external connection when defining it.
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-21 13:54:01
| Show all posts
Sorry, I'm wrong. The above definition can be used without extern, ... const.
Reply

Use magic Report

0

Threads

36

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-22 01:00:01
| Show all posts
For details, please refer to my blog, which has the usage of CONST.
http://blog.csdn.net/lklklk/posts/1464574.aspx


const principle: It is best to use const in C ++ to modify the type before it.
Such as: const int a; and int const b ;, although the two have the same meaning, it is best to use the latter case, the latter is more readable, because: int const we can clearly see that const is Modify int, and in the previous method, we don't know the specific meaning of const so easily. From the latter method we can know that const refers to a constant integer. Int * const b refers to a constant pointer b. This pointer points to an integer, so the content of this pointer can be changed, but its pointer value, that is, the value of b cannot be changed. The corresponding int const * b, refers to Is a pointer b to the constant integer content, that is to say, the content of this b can be changed, but the content of the address pointed by the beginning b cannot be changed in the program through b.
volatile also applies to the above principles.
Reply

Use magic Report

0

Threads

36

Posts

13.00

Credits

Newbie

Rank: 1

Credits
13.00

 China

Post time: 2020-1-22 04:27:01
| Show all posts
extern const int&ri;
So it is a definition of a constant data reference, and the value of the reference remains unchanged
Reply

Use magic Report

2

Threads

54

Posts

34.00

Credits

Newbie

Rank: 1

Credits
34.00

 China

Post time: 2020-1-22 20:54:01
| Show all posts
extern statement
About const:
    1. More discussion of pointers
          const int * p =&ci1;
          p =&ci2;
          This is a pointer to a const object, and p is a pointer variable, which can point to ci1 or change to ci2

          After writing const after *, it is a constant pointer. P is a pointer constant. It is initialized when defined. You cannot change the pointer later.
          int * const p =&i1;
          p =&i2; // err

          The combination of the two
          const int * const p =&ci3;

    2. Cases involving typedef
          First say an opinion
          Defining constants can be const int ci1 = 0;
                          Can int const ci2 = 0;

          typedef char * C;
          const C p =&ch;
          What is this p, is it const char * p or char * const p;
          Is the latter,
          Reason: C is a type in const C p, and const is a modifier of p
        
          So based on this, I personally recommend that when writing a const quantity definition, use the form int const ci2 = 0; to explicitly state that const modifies ci2
          and so,
          typedef char * C;
           C const p =&ch;
Reply

Use magic Report

0

Threads

24

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-25 14:27:02
| Show all posts
Should be a statement
Reply

Use magic Report

0

Threads

3

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-25 22:45:01
| Show all posts
Should be a statement
Reply

Use magic Report

0

Threads

12

Posts

10.00

Credits

Newbie

Rank: 1

Credits
10.00

 Hong Kong

Post time: 2020-1-25 23:45:01
| Show all posts
statement
extern statement
If it was a definition, it would have clearly pointed to an object.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list