博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C++ 引用和指针作为函数参数的例子。请不要拍砖

Posted on 2011-07-21 15:57  钟悍  阅读(546)  评论(0编辑  收藏  举报
#include <iostream>
#include 
<string>
using namespace std;

void referenceTest(int &a, int &b)
{
    a
=4;
    b
=5;
}
void pointerTest(string *a, string *b)
{
    
string kk="abc";
    a
=&kk;
    
*b="5";
}
                 
int main()
{
    
int x=0, y=0;
    referenceTest(x,y);
    cout
<<"x="<<x<<",y="<<y<<endl;

    
string a="0", b="0";
    
string *aa=&a, *bb=&b;
    pointerTest(aa,bb);
    cout
<<"aa="<<*aa<<",bb="<<*bb<<endl;   // out put: aa=4, bb=5
    cout
<<"a="<<a<<",b="<<b<<endl;         // out put:a=0,b=5  

    
return 0;
}