C++类中的成员是一个指针,我该如何访问,请大佬解惑!

C++类中的成员是一个指针,我该如何访问,请大佬解惑!

问题描述:

#include <iostream>
#include<string>
using namespace std;
class Person {
private:
	
public:
	//传统初始化
	string name;
	int age;
	int *height;
	Person(string a, int b, int c) {
		name = a;
		age = b;
		height =new int(c);

	}


};
void test01() {

	Person p("haha", 1, 2);
	cout << p.age << endl;
	cout << p.name << endl;
	cout << p.height << endl;

}
int main() {

	test01();

	system("pause");
	return 0;
}

cout << p.height << endl;这一句只能显示内存地址,如果我想显示他本身代表的值应该如何操作,请问?

我是C++初学者,p.*height,p->height,p->*height这些写法都报错,请问该如何解决,谢谢~~~

cout << *p.height << endl;

cout << *p.height << endl;

height是指针类型啊,和age不一样,所以直接打印height的话,就是个指针地址了,要打印值的话,就要取地址的值进行打印

cout << *(p.height) << endl;

谢谢楼上的大佬们!!!

也许对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html