C + + Learning notes and thoughts--const qualifiers and their references, typedef symbols

Source: Internet
Author: User

1. Defining a Const object
Because constants cannot be modified after they are defined, they must be initialized at the time of definition.

const int Bufsize=512;const int i; Error I is uninitialized const

2. Const object defaults to local variables in the file
We know that if we define a non-const variable in a file, if we want to use that variable in another file, we just need to add the extern declaration before using it.

File1.cppint counter;//File2.cppextern int counter;++counter; non-const variable defaults to extern. However, the Const object defaults to a local variable of the file, so if you want the const variable to be used in another file, you must explicitly designate it as extern//file1.cppextern const int counter=0;// File2.cppextern const int counter;for (int i=0;i<counter;i++) {}

Const reference
Common errors:
1. Normal references only bind the object at initialization time (the constant is wrong).
2. Binding a normal reference to a const object is not legal.
3.const references can be initialized to different types of objects or initialized to rvalue values.

#include <iostream>using namespace Std;int main () {int a = 0;const int bb = -1;int &c = 3;//error 1const int &am P;d = 3;int &ff = bb;//error 2const int &e = a;const int &f = Bb;const double &y = A; 3return  0;}
We went on to understand 3, why is the const int &d=3 correct?
for int &d=3, if we do d++, then it can be regarded as 3++, which is obviously wrong. However, if the const int &d=3, because const is read-only, the 3++ situation does not appear, so it is allowed.

typedef
Definition: It gives an alias to an already existing type within its own scope. There are four common effects:
Function One:

typedef can be used to define synonyms for a type.
typedef int size;
Size ARRAY[10];

Function Two:

typedef can also disguise composite types, such as pointers and arrays
If I want to define multiple arrays of size 30, you don't have to
Char line[30]; Char text[30];
You only need to define this:
typedef char LINE[30]; The line type represents an array of characters with 30 elements
Line Line,text can be equivalent to the above operation.

Similarly, you can hide the pointer syntax as follows.
typedef char* Pstar;
(#define DStar char*)
But the typedef is not a simple replacement like define, he is a valid alias within the scope.
DStar PA,PB; It just declares a pointer to a character variable and a character variable
Pstar pa,pb;//declares two pointers to character variables

Note here:
The meaning of const Pstar p is different from that of the const DStar p.
The first represents a const pointer, which is equivalent to char * const p; its pointer cannot be changed;
The second represents a pointer to a const object, and the content that P points to cannot be changed.
Role three:
Used in the code of the old C to help the struct. In the previous code, when declaring a new object of a struct, it was necessary to bring a struct, that is, the name of the struct struct name object, which is cumbersome to use in a large number of ways. So there it is.
typedef struct NODE
{
int x;
int y;
}point; Point A;
This form of notation omits the struct. But in C + +, this is not a common notation. We are mastering these to better read the previous code.
Role four:
typedef & Complex Variable declarations
>1:int * (*a[5]) (int, char*);
>2:void (*b[10]) (void (*) ());
>3. Doube (*) () (*PA) [9];


>1:int * (*a[5]) (int, char*);
The simple way to create a type alias for a complex variable is simply to replace the variable name with a type name (alias) in a traditional variable declaration expression, and then add the keyword typedef to the beginning of the statement.

Pfun is a type alias we built.
typedef int * (*PFUN) (int, char*);
Declares an object with a new type of definition, equivalent to int* (*a[5]) (int, char*);
Pfun A[5];

>2:void (*b[10]) (void (*) ());
First, declare a new type for the blue part of the expression above
typedef void (*pfunparam) ();
Declare a new type as a whole
typedef void (*pfun) (Pfunparam);
Declares an object with a new type of definition, equivalent to void (*b[10]) (void (*) ());
Pfun B[10];

>3. Double (*) () [1] (*PA) [9][2];
First, declare a new type for the blue part of the expression above
typedef double (*pfun) ();
Declare a new type as a whole
typedef pfun (*pfunparam) [9];
Declares an object with a new type of definition, equivalent to a double (*) () (*PA) [9];
Pfunparam PA;
The PA is a pointer to an array that has 9 elements, each of which is "Doube (*) ()"-or a pointer to a function with null function arguments and a "double" return value.

#include <iostream>using namespace Std;char s[2]={' A ', ' B '};int f (int x) {X+=5;return x;} int* ff (int x) {X+=1;return &x;} void ff2 (int x) {cout << "executed" << Endl;} typedef char* PSTAR;INT Main () {int (*p) (int x);//Declare a function pointer void (*A[1]) (int) = {FF2};//define a function pointer array (*a[0]) (1);//Call---Resul T: already performed p=f;cout<< (*p) (1) <<endl; result:6cout<<* (FF (2)) <<endl;//result 3const Pstar nm=s;return  0;}

In addition, the error is that typedef is syntactically a keyword for storing classes (like auto, extern, mutable, static, register, etc.), although it does not really affect the storage characteristics of objects, such as:
typedef static INT INT2; Not feasible
Compilation will fail with the hint "more than one storage class specified"

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Learning notes and thoughts--const qualifiers and their references, typedef symbols

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.