// write a point array in a file
#include <iostream>
#include <string>
using namespace std;
class person
{
public:
string name;
int age;
string address;
string phone;
string email;
public:
person()
{
name = "Tony";
age = 0;
address = "123";
phone = "123";
email = "123";
cout << "Address of the current object "<<this << endl;
}
};
int main()
{
person p1;
//person p2;
person *q[2];
person **q1;
q[0] = &p1;
//q[1] = &p2;
q1 = q;
//Address of the current object
cout << "p1 address " << &p1 << endl;
cout << "q[0] value " << q[0] << endl;
cout << "*q1 value " << *q1 << endl;
cout << "q1[0] value " << q1[0]<< endl;
//可以深入了解对象调用方法属性的机制:只要有了当前对象的地址,就可以调用对象的方法属性
//因为对象创建的时候会有this指针默认创建,所以可以直接调用对象的方法属性
//以上的表示方法都可以存入对象的地址,所以可以直接用q1[0]调用对象的方法属性
cout << "------------------------------" << endl;
// cout << *q1->name << endl;//这个不能解析是因为*q1是具体的值不是指针了
cout << q[0]->name << endl;
cout << q1[0]->name << endl;
cout << p1.name << endl;
//而因为数组的本质就是指针,方括号等于解引用,看下例子
char b = 'c';
char *a = &b;
cout << "*a is " << *a << endl;
cout <<"a[0] is "<< a[0] << endl;
cout << "------------------------------" << endl;
cout << "q address " << q << endl;
cout << "q1 value " << q1 << endl;
}
c++指向对象的双重指针不需要解引用用数组下标访问所指对象属性和方法的问题
于 2022-03-09 19:06:56 首次发布