C++的指针,引用,指向指针的引用和Java中的引用
2024-09-07 10:32:00
#include <iostream>
#include<algorithm>
using namespace std;
class Test {
public:
Test(int num) {
this->num = num;
}
public:
int num;
};
//引用
void change0(Test& t) {
t.num = 2;
Test temp(3);
t=temp;
Test test(4);
t=test;
// t = new Test(3); // 报错
}
//指向指针的引用
void change1(Test* & t) {
t->num = 2;
t = new Test(3);
}
//指针
void change2(Test *t) {
Test *a=t;
cout<<t<<" "<<&t<<" "<<(*t).num<<endl;//t表示指针里存的值即指向的对象的地址,&t表示指针自身的地址,*t表示指向的对象的值
t->num = 2;
t = new Test(3); // 指针再次赋值,指向新内存
}
int main(int argc, char const *argv[])
{
Test t3(1);
cout << "------引用---------" << endl;
cout << t3.num << endl;
change0(t3);
cout << t3.num << endl;
Test *t2 = new Test(1);
cout << "------指针---------" << endl;
cout << t2->num << endl;
change2(t2);
cout << t2->num << endl;
Test *t = new Test(1);
cout << "------指针的引用---------" << endl;
cout << t->num << endl;
change1(t);
cout << t->num << endl;
system("pause");
return 0;
}
最新文章
- 怎样设置才能允许外网访问MySQL
- 2016HUAS暑假集训训练2 F - A Simple Problem with Integers
- 解决 RHEL 7/ CentOS 7/Fedora 出现Unit iptables.service failed to load
- PHP7革新与性能优化
- noi 7627 鸡蛋的硬度
- 调试mvc 源码【转:http://www.cnblogs.com/wucj/archive/2013/06/09/3128698.html】
- Excel数据链接取消
- Lua运算符
- HDU 4005 The war (图论-tarjan)
- ValueError: Cannot feed value of shape ..
- 牛客练习赛35-背单词-线性DP
- 最全的MonkeyRunner自动化测试从入门到精通(9)
- epoll的由来
- Java数组逆序排列
- Nginx安装使用及与tomcat实现负载均衡
- telnet 163发送邮件
- SQL中Union和UnionAll的使用
- python-day11--函数
- exception processing, template error resolving template
- uva 701 - The Archeologists&#39; Dilemma
热门文章
- A - 敌兵布阵 ——B - I Hate It——C - A Simple Problem with Integers(线段树)
- LianLianKan HDU - 4272 状压dp
- - 迷宫问题 POJ - 3984 bfs记录路径并输出最短路径
- WPF 只读集合在 XAML 中的绑定(WPF:Binding for readonly collection in xaml)
- K8S(04)核心插件-coredns服务
- 二进制方式安装docker(非root用户启动docker)
- Java RMI 实现一个简单的GFS(谷歌文件系统)——介绍篇
- RT-Thread学习笔记2-互斥量与信号量
- ES6 version repeatify
- HTML Custom Elements (v1)