C++ 中关于指针数组

汗一个,今天上午因为#define N 5 后面加上双引号了,找了半天错误……

 

字面意思来讲,数组里面每一个元素都是指向某一类型对象的指针

由于指针数组对象每一个元素的类型为T*,(T *a = new T)

所以,指向这个数组对象的元素对象的指针的类型为T**

 

代码如下:

#include "stdafx.h" #include <iostream> #include <string> using namespace std; class Person{ private: int height ; int width ; public: void setHeight(int h){this->height = h;} void setWidth(int w){this->width = w;} int getHeight(){return this->height;} int getWidth(){return this->width;} }; void f(Person **a,int n); int main(){ Person *p[2]; p[0] = new Person; p[0]->setHeight(15); p[1] = new Person; p[1]->setHeight(10); f(p,2); return 0; } void f(Person **a,int n){ for(int i=0;i<n;i++){ int s = a[i]->getHeight(); cout<<s<<endl; } }

你可能感兴趣的:(C++ 中关于指针数组)