The mechanisms of overloading, inline, default parameters, and implicit conversion in C + + language show many advantages, but there are some hidden dangers behind these advantages. Just as people's diets, eating less and overeating are undesirable, should be right. We should look at the new mechanisms of C + + in a dialectical way and use them appropriately.
Although this will make us spend more time programming, less fun, but this is the art of programming.
1#include <iostream>2 3 /*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/4 using namespacestd;5 intMainintargcChar**argv) {6 //defines a struct type that contains pointer members7 structTest {8 Char*str;9 int*IP;Ten } x; One A //use integer pointer ip in struct variable x -x.ip=New int;//Allocate 1 units -* (X.IP) = -; thecout<<"X.ip:"<<x.ip<<'\ t'<<* (X.IP) <<Endl; -cout<<"---------------"<<Endl; - DeleteX.ip; -x.ip=New int[5];//Allocate 5 units + for(intI=0;i<5; i++) -* (x.ip+i) = -+i; +cout<<"X.ip:"<<Endl; A for(intI=0;i<5; i++) atcout<<x.ip+i<<'\ t'<< (* (x.ip+i)) <<Endl; - DeleteX.ip; -cout<<"---------------"<<Endl; - - //Use the character-type pointer in structure variable x str -X.str=New Char('A');//Allocate 1 units incout<<"X.str:"<< (*X.STR) <<Endl; -cout<<"---------------"<<Endl; to DeleteX.str; +X.str=New Char[5];//Assigning multiple cells -*x.str='G'; the* (x.str+1)='o'; ** (x.str+2)='o'; $* (x.str+3)='D';Panax Notoginseng* (x.str+4)=' /'; -cout<<"X.str:"<<x.str<<Endl; the DeleteX.str; +cout<<"---------------"<<Endl; A the //initializing when declaring a struct variable +Test y={"Very good!", NULL}; -cout<<"Y.str:"<<y.str<<Endl; $cout<<"Y.ip:"<<y.ip<<Endl; $ return 0; -}
The mechanisms of overloading, inline, default parameters, and implicit conversions in the C + + language show many advantages