C++基础(十四)C++ 的 ->的应用:访问结构体、访问类成员、访问指针变量

参考:https://zhidao.baidu.com/question/363341293.html

一、访问结构体

#include<stdio.h>
struct stu   // 定义一个结构体
{
    char name[10];  // 姓名
    int num;  // 学号
    int age;  // 年龄
};
void main()
{
    struct stu *s;   // 定义一个结构体指针
    char str[]="ZhangLi";
    s->name = str;     // 对结构体中的成员变量name进行赋值
    s->num = 2015120;  // 对结构体中的成员变量num进行赋值
    s->age = 18;       // 对结构体中的成员变量age进行赋值
}

二、访问类成员

class Memory
{
   public:
      void destroy ();
      void create();
      int a;
}
Memory* memory = new Memory();
memory->destory();
int b = memory->a;

猜你喜欢

转载自blog.csdn.net/xpj8888/article/details/85838148