指针的引用(*&)

来源:互联网 发布:python 网管snmp 编辑:程序博客网 时间:2024/06/01 07:32

Void TEncSlice::compressSlice( TComPic* & rpcPic )函数参数传递的是指针的引用
函数参数传递有三种方法,值传递,址传递,传递引用;
这里说的是传递指针的引用。
Void TEncSlice::compressSlice( TComPic* rpcPic )————————传址
Void TEncSlice::compressSlice( TComPic & rpcPic )———————引用
Void TEncSlice::compressSlice( TComPic rpcPic )———————传值
传址和引用都可以改变原来的值,而传值只是副本,无法改变原来的值。
指针的引用可以改变指针的内容,也就是地址。。。。。

首先看下引用,引用就是变量的别名,当函数变量是int类型的变量a的引用,那么可以写为
Void TEncSlice::compressSlice( int &a)
同样是引用,当引用的对象时指针呢?是不是也可以和int类型变量一样考虑呢?我的理解是肯定的
Void TEncSlice::compressSlice( int* &a )可不可以理解为(int*)为一个整体?他就是代表引用的对象为指向整型变量的指针。

理解指向指针的引用

#include <iostream>using namespace std;void FreePtr1(int* ptr1){    delete ptr1;    ptr1 = NULL;}void FreePtr2(int*& ptr2){    delete ptr2;    ptr2 = NULL;}void FreePtr3(int **ptr3)//传指针的指针{    delete *ptr3;    *ptr3 = NULL;}void main(){    cout << "---------------------------------------" << endl;    int *p1 = new int;    *p1 = 1;//不是空指针    cout << "*p1=" << *p1 << endl;//值    FreePtr1(p1);//不会改变原来p1的内容(地址)    cout << "after call freePtr1" << endl;//    if (p1 != NULL)//会执行    {        cout << "p1 is not null" << endl;        cout << "*p1=" << (*p1) << endl;    }    cout << "---------------------------------------" << endl;    int *p2 = new int;    *p2 = 2;    cout << "*p2=" << *p2 << endl;//值    FreePtr2(p2);//改变了p2的内容    cout << "after call freePtr2" << endl;    if (p2 != NULL)    {        cout << "*p2=" << *p2 << endl;    }    else//执行    {        cout << "the p2 is null" << endl;    }    cout << "---------------------------------------" << endl;    int *p3;    p3 = new int(3);    cout << "*p3=" << *p3 << endl;    FreePtr3(&p3);//取指针的地址为参数    cout << "after call freePtr3" << endl;    if (p3 != NULL)    {        cout << "*p3=" << *p3 << endl;    }    else    {        cout << "the p3 is null" << endl;    }    cout << "---------------------------------------" << endl;    system("pause");}

加入这段代码之后应该更好理解了!!结果图

0 0