c++ 参数引用传递

复制代码
 1 #include <iostream>
 2 #include <thread>
 3 #include<windows.h>
 4 using namespace std;
 5 void A(int& a) {
 6     cout <<"address" << &a << endl;;
 7     cout << "value" << a << endl;;
 8     a = 100;
 9 
10 }
11 int main() {
12     int b = 10;
13     cout << "address b is :" << &b << endl;;
14     cout << "********"<<endl;
15     A(b);
16     cout << b;
17 
18     return 0;
19 }

复制代码

要是直接在参数中传入指针可不可以呢,下面是示例代码,是不可以的

复制代码
#include <iostream>
#include <thread>
#include<windows.h>
using namespace std;
void A(int& a) {
    cout << "address" << &a << endl;;
    cout << "value" << a << endl;;
    a = 100;

}
int main() {
    int b = 10;
    int* c = &b;
    cout << "address b is :" << &b << endl;;
    cout << "********" << endl;
    A(b);
    cout << b;
    //A(c);//这样会报错
    A(*c);//这样就不会
    return 0;
}
复制代码

 

posted @   依然依然的a  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示