What are the details that should be paid attention to in C++?
This article introduces the details that we need to pay attention to in C++, the content is very detailed, interested friends can use it for reference, I hope it can be helpful to you.
Namespace uses # include using namespace std;// to define the namespace namespace NameSpaceA {int a = 0;} namespace NameSpaceB {int astat1; namespace NameSpaceC {struct Teacher {/ * data * / char name [10]; int age;};} int main () {/ / using namespace NameSpaceA; using NameSpaceB::NameSpaceC::Teacher; using namespace NameSpaceB Printf ("a =% d\ n", a); printf ("a =% d\ n", NameSpaceA::a); Teacher T1; t1.age = 20; printf ("age:% d\ n", t1.age); return 0 } const uses: / / C++ compiler's handling of const constants / / when you see a constant declaration, it puts a constant in the symbol table-- > when you want to use it, directly take the value in the symbol table to replace the const variable in / / C is a read-only variable, but its own storage space / / C++-- > const is global or & will allocate space / # define and const have something in common: the const modifier in C++ is a real constant. And C is just a read-only variable. The constants of const modification are determined during compilation. / / # define differs from const: # define-- > is handled by a preprocessor, simple text substitution / / const---- > compiler processing, providing type checking and scope checking # include using namespace std;void fun1 () {# define a 10 const int b = 20;} void fun2 () {printf ("% d\ n", a) / / cout cannot be the initial value of other references, the space is freed 2 C++ chain programming, operator overload * / / * pointer references-- > just to store individual names in this block of pointers and * / # include using namespace std;// proves that the reference has its own memory space struct Teacher {/ * data * / int & a; int & b;} Int main2 () {printf ("sizeof (Teacher):% d\ n", sizeof (Teacher)); / / 8 int-> int const * an and int const * b return 0;} int main1 () {int a = 10; int & b = a; printf ("& aVue% d\ n", & a); printf ("& bJV% d\ n", & b); / / can I have multiple names for the same memory space? -- > No / / actually: int const * b = & a & b: the compiler will do * (& b) return 0;} / / reference to do function return value int F1 () {int a; a = 10; return a;} int & f2 () {int a; a = 20; return a;} int * f3 () {int a; a = 30; return & a } /-> harm, in fact, it's just a sentence to see if the memory has been released / / static + & A reference to int j () / /-> returns only a number {static int a = 10; astatum; printf ("alun% d\ n", a); return a;} int & j2 () / / return * (& a) {static int a = 20; astatum + Printf ("aint% d\ n", a); return a;} int * j3 () / /-> returns an address {static int a = 30; aplomb; printf ("av% d\ n", a); return & a;} int main33 () {/ / j ()-> returns a numeric value and cannot be regarded as the left value 11 = 100 (err) j2 () = 100; J2 () / / manually crafted reference conditions * (j3 ()) = 100; j3 (); getchar (); return 0; / / non-basic type: problems that may be designed to deep and shallow copies-- > that is, whether there is memory space or not: struct Student {int age;}; Student & Op (Student & s) {/ * data * / s.age = 30;} int main44 () {Student S1 S1.age = 20; Op (S1); cout but a can change / / b = 11; a = 20; int * p = (int *) & b; / / const int aa = 20; const int & bb = aa; / / int & b = aa;} / / const: constant competition variable-- > const int * const pint main () {const int b = 10; printf ("& b% aa;\ n", & b) Const int & bb = 19; printf ("& bb:%d\ n", & bb); getchar (); return 0;} trinomial operation / / in C++, the trinomial operation returns the variable itself, and you can do the left value # include using namespace std;int main () {int a = 20; int b = 30; a = (a)