这篇博文主要介绍了如何在C++11 VS2013编译器上去实现各种引用,避免因为编译器不同造成的语法困难
1:引用数组,二维数组 ,数组指针
#include <iostream>
using namespace std;
void main1(){
/*int a[3]{1,2,3};
int aa[2][3]{1,2,3,4,5,6};
int*pa(new int[3]{1,2,3});
int(*paa)[3](new int[2][3]{1,2,3,4,5,6});*/
//int(*paa)[3] = new int[2][3]{{ 1, 2, 3 }, { 4, 5, 6 }};
int a[3]{1,2,3};
int* pa[3] = { a, a + 1, a + 2 };
int(&ra)[3](a); //引用一个数组
int*(&raa)[3](pa); //引用一个指针数组
for (auto i : ra){
cout << i << endl;
}
for (auto i : raa){
cout <<*i<< endl;
}
cin.get();
}
void main2(){
//堆上的一维数组
int*p1(new int[3]{22, 33, 44});
int*(*p2)(new int*[3]{p1,p1+1,p1+2});
//引用指针
int(*(&fp))(p1);
int(**(&fpp))(p2);
for (int i = 0; i < 3;i++){
cout <<fp[i] <<endl;
}
for (int i = 0; i < 3; i++){
cout << *fpp[i] << endl;
}
cin.get();
}
void main3(){
int pp[2][3]{33,44,55,11,22,99};
int* ppp[2][3]{&pp[0][0],&pp[0][1],&pp[0][2],&pp[1][0],&pp[1][1],&pp[1][2]};
//引用二位数组
int(&fp)[2][3](pp);
//引用指针二位数组
int*(&fpp)[2][3](ppp);
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++){
cout << fp[i][j] << endl;
}
}
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++){
cout << *fpp[i][j] << endl;
}
}
cin.get();
}
void main5(){
//堆上二维数组
int(*p)[3] = new int[2][3]{{22,33,44},{55,66,77}};
//int*(*p)[3] = new int*[2][3]{{&p[0][0],&p[0][1],&p[0][2]}, {&p[1][0],&p[1][1],&p[1][2]}};
//引用二维数组指针
int(* &fp)[3](p); //引用一个行指针
for (int i = 0; i < 2; i++){
for (int j = 0; j < 3; j++){
cout << fp[i][j] << endl;
}
}
cin.get();
}
二:函数指针的引用,包括返回值是函数指针
三:函数指针数组的引用#include <iostream> using namespace std; int showcmd(char* cmd){ cout << cmd << endl; return 0; } int changeCmd(char* cmd){ cout <<"ShaoZHongqi"<< endl; return 0; } //引用函数指针进行切换 void changeFunc(int(* &myfun)(char* cmd)){ myfun = changeCmd; } //返回值是函数指针的引用 int(*& getMyFun())(char* cmd){ //返回一个函数指针 int(*p)(char* cmd) = showcmd; return p; } //返回函数指针 int(* getRFun( int(*& myfun )(char* cmd) ))(char* cmd){ myfun = changeCmd; return myfun; } //返回函数指针的引用 int(*& getRFun2(int(*& myfun)(char* cmd)))(char* cmd){ myfun = showcmd; return myfun; } int(*& getMyRFun())(char* cmd){ int(*& pp)(char* cmd)((int(*&)(char* cmd))(showcmd)); return pp; } void maintest2(){ int(*fun)(char* cmd)(showcmd); changeFunc(fun); fun = getMyFun(); //fun("Beijing22"); getRFun2(fun)("Nanjing2222"); getMyRFun()("123"); cin.get(); }
四:引用结构体数组#include <iostream> using namespace std; int add(int a, int b){ return a + b; } int plusp(int a,int b){ return a - b; } int dubplite(int a, int b){ return a*b; } void maintest(){ int(*fun[3])(int, int){add,plusp,dubplite}; //二级函数指针数组 int(**funarray)(int, int){ new (int(*[3])(int, int)){ add, plusp, dubplite} }; //引用函数指针 int(* (&pf)[3])(int, int)(fun); int(** &pff)(int, int)(funarray); for (int i = 0; i < 3; i++){ cout << pf[i](100, 10) << endl; } //遍历函数指针数组 for (int(** px)(int, int) = pff; px < pff + 3;px++){ cout << (*px)(100, 10) << endl; } cout << "Nanjing" << endl; cin.get(); }
#include <iostream> using namespace std; struct info{ int id; int num; info(int nid,int nnum):id(nid),num(nnum){ } }; void main(){ struct info infoarray[3]{{1,11}, {2,22}, {3,33} }; for (auto i : infoarray){ cout << i.id << "====" << i.num << endl; } struct info(&finfo)[3](infoarray); for (auto i : finfo){ cout << i.id << "====" << i.num << endl; } info* pinfoarray{ new info[3]{{ 1, 11 }, { 2, 22 }, {3,33} } }; for (info*p = pinfoarray; p < pinfoarray + 3;p++){ cout << p->id << "====" << p->num << endl; } cout << "Nanjing" << endl; cin.get(); }