c++中->和.两种访问方式的区别

c++中->和.两种访问方式的区别

  • 如果A是类的实例化对象结构体,则使用A.B的形式进行成员访问
  • 如果A是指向结构体联合体的指针,则使用A->B的形式进行成员访问
class student
{
    
    
	public:
		int a;
}
  • 两种访问方式
student x;
student *p = x;
//通过指针形式访问student类的成员
p->a;
//但是如果对指针进行解引用,就是该类的实例化对象,就可以通过下面方式进行访问
*p.a;
//如果是直接对类,结构体,联合体的成员进行访问
x.a;
  • 结构体数组的两种访问方式
struct student
{
    
    
	int age;
	char name[100];
};
struct student array[3];
  • 两种访问方式
struct student array[3];
//通过指针方式
struct student *p = array;
p->age = 30;
//通过结构体的方式
array[0].age = 30;

猜你喜欢

转载自blog.csdn.net/m0_45388819/article/details/113817791