当前位置: 首页 > ai >正文

2025.8.2

this指针代码练习
//用指针访问对象成员
#include<iostream>
using namespace std;
class MyClass
{
public:
MyClass(int a, int b) {
this->a = a;
this->b = b;
}
int getA() {
return this->a;
}
private:
int a;
int b;
};
int main() {
MyClass myclass(2, 3);
cout << "a的值是:" << myclass.getA() << endl;
return 0;
}


//this指针作为返回值
#include<iostream>
using namespace std;
class MyClass
{
public:
MyClass(int a, int b) {
this->a = a;
this->b = b;
}
MyClass* getAddr() {
return this;
}
MyClass getCopy() {
return this*;
}
int getA() {
return a;
}
private:
int a;
int b;
};
int main() {
MyClass myclass(2, 3);
MyClass* ptr = myclass.getAddr();
cout << "a的值是:" << ptr->getA() << endl;
MyClass copy = myclass.getCopy();
cout << "a的值是:" <<copy.getA() << endl;
return 0;
}

http://www.xdnf.cn/news/16843.html

相关文章:

  • VScode对Ubuntu用root账号进行SSH远程连接开发
  • 文心4.5开源测评:国产大模型的轻量化革命与全栈突破
  • 每日五个pyecharts可视化图表-bars(1)
  • SpringBoot启动项目详解
  • 详解Python标准库之命令行界面库
  • JavaScript特殊集合WeakMap 的使用及场景介绍
  • 未来交通:元宇宙技术重塑出行体验
  • SLAM中的非线性优化-2D图优化之零空间实战(十六)
  • Selenium自动化:轻松实现网页操控
  • 归并排序(简单讲解)
  • MySQL 基础
  • linux source命令使用详细介绍
  • 浅拷贝与深拷贝的区别
  • Vue 响应式基础全解析2
  • Python Pandas.unique函数解析与实战教程
  • 24黑马SpringCloud的Docker本地目录挂载出现相关问题解决
  • 《JMM 与 happens-before 原则:并发编程的核心内存语义》
  • 网络常识-子网掩码
  • 暑期算法训练.13
  • stm32F407 实现有感BLDC 六步换相 cubemx配置及源代码(二)
  • 电脑系统中的BCD
  • 排序算法-堆排序
  • ARMv8/v9架构FAR_EL3寄存器介绍
  • Android 13/14/15 默认授权应用权限的实现方法
  • 《深潜React列表渲染:调和算法与虚拟DOM Diff的优化深解》
  • 开疆智能Profinet转Modbus网关连接信捷PLC从站配置案例
  • WPFC#超市管理系统(4)入库管理
  • oect刷入arm系统安装docker
  • 【Redis数据结构详解】特点、用途与实际应用
  • CCF IVC 2025“汽车安全攻防赛” -- Crypto -- WriteUp