C++入门03 -- 类、class、struct、对象、thi

2021-08-11  本文已影响0人  YanZi_33

C++的编程规范

#include <iostream>

using namespace::std;

struct Person {
    //成员变量
    int age;
    
    //成员函数
    void run() {
        cout << "run() age = " << age <<endl;
    }
};

class Student {
public:
    int no;
    
    void study() {
        cout << "study() no = " << no <<endl;
    }
};

int main(int argc, const char * argv[]) {
    //已经创建了一个对象 已经分配了内存空间
    //person对象在栈区分配内存
    Person person1;
    person1.age = 30;
    person1.run();
    
    Person person2;
    person2.age = 33;
    person2.run();
    
    Student stu;
    stu.no = 10;
    stu.study();
    
    return 0;
}

指针访问对象成员的本质

#include <iostream>

using namespace::std;

struct Person {
    //成员变量
    int m_age;
    int m_width;
    int m_height;
    
    //成员函数
    void run() {
        cout << "run() age = " << m_age <<endl;
    }
};

int main(int argc, const char * argv[]) {
    //person对象在栈区分配内存
    Person person;
    person.m_age = 1;
    person.m_width = 2;
    person.m_height = 3;
    person.run();
    
    //定义一个指针变量 指向person对象
    Person *ptr = &person;
    ptr->m_age = 4;
    ptr->m_width = 5;
    ptr->m_height = 6;
    ptr->run();
    
    cout << sizeof(person) << endl;
    cout << "&person = " << &person << endl;
    cout << "&person.m_age = " << &person.m_age << endl;
    cout << "&person.m_width = " << &person.m_width << endl;
    cout << "&person.m_height = " << &person.m_height << endl;
    
    return 0;
}
Snip20210811_139.png
C++07_类`main:
    0x100002ff0 <+0>:   pushq  %rbp
    0x100002ff1 <+1>:   movq   %rsp, %rbp
    0x100002ff4 <+4>:   subq   $0x50, %rsp
    0x100002ff8 <+8>:   movl   $0x0, -0x4(%rbp)
    0x100002fff <+15>:  movl   %edi, -0x8(%rbp)
    0x100003002 <+18>:  movq   %rsi, -0x10(%rbp)
    0x100003006 <+22>:  movl   $0x1, -0x20(%rbp)
    0x10000300d <+29>:  movl   $0x2, -0x1c(%rbp)
    0x100003014 <+36>:  movl   $0x3, -0x18(%rbp)
    0x10000301b <+43>:  leaq   -0x20(%rbp), %rdi
    0x10000301f <+47>:  callq  0x100003170               ; Person::run at main.cpp:19
    0x100003024 <+52>:  leaq   -0x20(%rbp), %rax
    0x100003028 <+56>:  movq   %rax, -0x28(%rbp)
    0x10000302c <+60>:  movq   -0x28(%rbp), %rax
    0x100003030 <+64>:  movl   $0x4, (%rax)
    0x100003036 <+70>:  movq   -0x28(%rbp), %rax
    0x10000303a <+74>:  movl   $0x5, 0x4(%rax)
->  0x100003041 <+81>:  movq   -0x28(%rbp), %rax
    0x100003045 <+85>:  movl   $0x6, 0x8(%rax)
    0x10000304c <+92>:  movq   -0x28(%rbp), %rdi

this指针

#include <iostream>

using namespace::std;

struct Person {
    //成员变量
    int m_age;
    int m_width;
    int m_height;
    
    //成员函数
    void run() {
        cout << "run() m_age = " << m_age <<endl;
        cout << "run() m_width = " << m_width <<endl;
        cout << "run() m_height = " << m_height <<endl;
        
        cout << "run() m_age = " << this->m_age <<endl;
        cout << "run() m_width = " << this->m_width <<endl;
        cout << "run() m_height = " << this->m_height <<endl;
    }
};

int main(int argc, const char * argv[]) {
    //person对象在栈区分配内存
    Person person;
    person.m_age = 1;
    person.m_width = 2;
    person.m_height = 3;
    person.run();
   
    return 0;
}
C++07_类`main:
    0x100003050 <+0>:  pushq  %rbp
    0x100003051 <+1>:  movq   %rsp, %rbp
    0x100003054 <+4>:  subq   $0x20, %rsp
    0x100003058 <+8>:  movl   $0x0, -0x4(%rbp)
    0x10000305f <+15>: movl   %edi, -0x8(%rbp)
    0x100003062 <+18>: movq   %rsi, -0x10(%rbp)
    0x100003066 <+22>: movl   $0x1, -0x20(%rbp)
    0x10000306d <+29>: movl   $0x2, -0x1c(%rbp)
    0x100003074 <+36>: movl   $0x3, -0x18(%rbp)
    0x10000307b <+43>: leaq   -0x20(%rbp), %rdi
->  0x10000307f <+47>: callq  0x100003090               ; Person::run at main.cpp:28
    0x100003084 <+52>: xorl   %eax, %eax
    0x100003086 <+54>: addq   $0x20, %rsp
    0x10000308a <+58>: popq   %rbp
    0x10000308b <+59>: retq   
上一篇 下一篇

猜你喜欢

热点阅读