[C++] 对象指针使用方法

对象指针:指向类对象的指针

类指针指向类变量(对象)的地址

对象指针定义格式:

举例:

#include 

using namespace std;

class Student {
private:
    int age;
public:
    void setAge(int n) {
        this->age = n;
    }

    int getAge() {
        return this->age;
    }
};

int main(void) {
    Student stu;
    Student *pStu = &stu;

    // 对象用.访问公有成员
    stu.setAge(12);
    // 对象指针用->访问共有成员
    cout << pStu->getAge() << endl;

    return 0;
}

Original: https://www.cnblogs.com/lialong1st/p/12013357.html
Author: LeeAaron
Title: [C++] 对象指针使用方法

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/535058/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球