【C++】指针作为参数传递的一个坑

【C++】指针作为参数传递的一个坑,第1张

【C++】指针作为参数传递的一个坑
#include 
#include 

void func1(int *pCnt)
{
    pCnt = new int(1);
}

void func2(int* &pCnt)
{
    pCnt = new int(2);
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    int *pCnt1 = nullptr;
    func1(pCnt1);
    if(nullptr != pCnt1)
        qDebug() << "*pCnt1: " << *pCnt1;
    else
        qDebug() << "pCnt1 nullptr";        // 输出这句


    int *pCnt2 = nullptr;
    func2(pCnt2);
    if(nullptr != pCnt2)
        qDebug() << "*pCnt2: " << *pCnt2;   // 输出这句
    else
        qDebug() << "pCnt2 nullptr";

    return a.exec();
}

// 运行结果:

pCnt1 nullptr
*pCnt2:  2

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5657489.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存