滴水三期:day42.1-面向对象编程及this指针说明

文章介绍了C++中通过结构体模拟面向对象编程的概念,包括结构体与类的关联,成员方法的参数传递,特别是this指针的使用和作用。通过实例展示了成员方法如何在结构体中定义和调用,并解释了编译器在背后如何处理成员方法的调用,包括参数传递和调用约定。此外,还讨论了空结构体的大小以及空指针调用成员方法的情况,强调了this指针在函数执行中的重要性。

一、面向对象编程

笔者由于学习过JAVA,所以对面向对象编程已经有一些自己的理解,下面不会讲解太详细

1.结构体<=>类

  • 先复习一下结构体作为参数传递:结构体作为参数传给函数,本质上是将结构体中所有成员复制一份传到函数的栈中。这样会比较浪费空间和时间

  • 所以我们会将结构体指针作为参数传递给函数:

    #include "stdafx.h"
    struct Base{
         
         
        int x;
        int y;
    };
    int Max(Base* basep){
         
           //结构体指针传参
        return basep->x + basep->y;
    }
    int main(int argc,char* argv[]){
         
         
        Base base;
        base.x = 1;
        base.y = 2;
      	printf("%d\n",Max(&base));
        printf("%d",sizeof(base));  //打印一下结构体的大小
        return 0;
    }
    
  • 这里我们查看一下现在结构体的大小:8字节。现在如果把Max函数放到Base结构体中,再看看是否报错,并且查看此时结构体的大小:

    #include "stdafx.h"
    struct Base{
         
         
        int x;
        int y;
    	int Max(Base* basep){
         
            //把Max函数放到Base结构体中
    		return basep->x + basep->y;
    	}
    };
    int main(int argc,char* argv[]){
         
         
        Base base;
        base.x = 1;
        base.y = 2;
      	printf("%d\n",base.Max(&base));  //这里调用函数时就需要用“结构体变量.”的方式调用
        printf("%d",sizeof(base));  //打印一下结构体的大小
        return 0;
    }
    
    • 函数作用范围受限:会发现完全可以把函数定义到结构体当中,但是在调用这个函数时需要把函数当成结构体中成员来调用:即base.Max()的方式来调用!
    • 结构体中函数不影响结构体大小:且发现结构体大小还是8字节!即Max函数当做结构体成员使用,但是却不包含在结构体当中
  • 综上:可以发现和面向对象的编程语言很像!结构体就是结构体中的变量就是成员变量结构体中的函数就是成员方法,使用Base base可以创建类的对象base,使用对象可以调用类中的变量和方法等,这些就是封装的思想

</

oot@admin123-NF5280M6:~# apt --fix-broken install Reading package lists... Done Building dependency tree... Done Reading state information... Done Correcting dependencies... failed. The following packages have unmet dependencies: gnome-settings-daemon : Depends: gnome-settings-daemon-common (= 42.1-1ubuntu2.2) but 46.0-1ubuntu1.24.04.1 is installed gnome-shell : Depends: gir1.2-gnomebg-4.0 but it is not installed Depends: gir1.2-gnomebluetooth-3.0 (>= 42.3) but 42.0-5 is installed Depends: gir1.2-gnomedesktop-4.0 (>= 40) but it is not installed Depends: gir1.2-gweather-4.0 (>= 4.1) but it is not installed Depends: gir1.2-mutter-14 (>= 46.0) but it is not installed Depends: gir1.2-nma4-1.0 but it is not installed Depends: gir1.2-soup-3.0 but it is not installed Depends: gir1.2-webkit-6.0 but it is not installed Depends: gnome-shell-common (= 46.0-0ubuntu6~24.04.12) but 42.9-0ubuntu2.3 is installed Depends: libatk-bridge2.0-0t64 (>= 2.5.3) but it is not installed Depends: libatk1.0-0t64 (>= 1.12.4) but it is not installed Depends: libecal-2.0-3 (>= 3.45) but it is not installed Depends: libedataserver-1.2-27t64 (>= 3.45) but it is not installed Depends: libgjs0g (>= 1.73.1) but 1.72.4-0ubuntu0.22.04.4 is installed Depends: libglib2.0-0t64 (>= 2.79.0) but it is not installed Depends: libgnome-desktop-4-2t64 (>= 40) but it is not installed Depends: libgtk-4-1 (>= 4.12.0) but 4.6.9+ds-0ubuntu0.22.04.2 is installed Depends: libical3t64 (>= 3.0.0) but it is not installed Depends: libmutter-14-0 (>= 46.0) but it is not installed Depends: libpipewire-0.3-0t64 (>= 0.3.10) but it is not installed Recommends: evolution-data-server (>= 3.45) but 3.44.4-0ubuntu1.1 is installed Recommends: gnome-control-center (>= 1:42) but 1:41.7-0ubuntu0.22.04.9 is installed gnome-shell-extension-desktop-icons-ng : Depends: gir1.2-gnomeautoar-0.1 but it is not installed gnome-shell-extension-ubuntu-dock : Depends: gnome-shell (< 43) but 46.0-0ubuntu6~24.04.12 is installed libatk-adaptor : Depends: libatk-bridge2.0-0t64 (>= 2.52.0-1build1) but it is not installed Depends: libglib2.0-0t64 (>= 2.62) but it is not installed libgck-2-2 : Depends: libglib2.0-0t64 (>= 2.79.0) but it is not installed libgcr-4-4 : Depends: libglib2.0-0t64 (>= 2.79.0) but it is not installed libpython3.12-minimal : Depends: libssl3t64 (>= 3.0.0) but it is not installed Recommends: libpython3.12-stdlib but it is not installed openssl : Depends: libssl3t64 (>= 3.0.9) but it is not installed thunderbird-locale-en : Depends: thunderbird (>= 2:1snap1-0ubuntu3) but 1:128.12.0+build1-0ubuntu0.22.04.1 is installed E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages. E: Unable to correct dependencies
12-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值