【共读Primer】43.[6.2]参数传递 Page187

参数传递的两种基本类型

值传递 (pass by value)

拷贝实参后赋值给形参,两个参数内容相同,但在内存中分别时不同的位置

以传值方式进行的函数调用如果在内部修改参数的值对实参不会有任何影响。

#include <iostream>
using std::cout;
using std::endl;
// 用传值方式进行传递,在函数内部对参数的操作不会影响到实参
void count(int co)
{
    ++co;
    cout << "args in count:" << co << endl;  
}

int main()
{
    int co = 1;
    count(co);
    count(co);
    count(co);
    cout << "arge in main:" << co << endl;
    return 0;
}

如果需要在内部改变参数的值,其中一种方式是使用指针来传递该值。

将上面的代码修改为

#include <iostream>
using std::cout;
using std::endl;
// 用指针的方式传递值,可以进行对实参的操作
void count(int* co)
{
    ++*co;
    cout << "args in count:" << *co << endl;  
}

int main()
{
    int co = 1;
    count(&co);
    count(&co);
    count(&co);
    cout << "arge in main:" << co << endl;
    return 0;
}

 

引用传递(pass by reference)

引用型参数时实参的引用,形参与实参进行了绑定,在内存中指向同一个位置。

对于上面的程序,如果觉得指针难于书写及使用我们可以选择引用的方式来进行对实参的修改。

#include <iostream>
using std::cout;
using std::endl;
// 用传引用的方式,可以对实参进行修改
void count(int& co) // 只在参数列表中加入引用符号,改动最小
{
    ++co;
    cout << "args in count:" << co << endl;  
}

int main()
{
    int co = 1;
    count(co);
    count(co);
    count(co);
    cout << "arge in main:" << co << endl;
    return 0;
}

使用引用的另一个好处是避免拷贝,提高函数的调用效率

// 在一般情况下,对于对象类型的参数我们使用引用。
// 因为引用不需要对元对象进行拷贝。
// 免除拷贝的意义在于即能减少内存的支出,又能提高效率一举多得
// 需要注意的是在不需要进行实参修改的情况下一定要加上const。
bool isSHorter(const string &s1, const string &s2)
{
    return s1.size() < s2.size();
}    

有时我们需要函数返回多个值,这个时候我们就需要借助到引用类型

#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;
// 用传引用的方式,可以对实参进行修改
void count(int& co) // 只在参数列表中加入引用符号,改动最小
{
    ++co;
    cout << "args in count:" << co << endl;  
}

// @brief 在字符串中查找字符,记录第一次出现的位置以及出现的次数
// 
// @param [in] const string &s 需要查找的目标内容
// @param [in] char c 查找的具体字符
// @param [out] string::size_type &occurs 字符出现的次数
string::size_type find_char(const string &s, char c, string::size_type &occurs)
{
    auto ret = s.size();
    occurs = 0;
    for (decltype(ret) i = 0; i != s.size(); ++i)
    {
        if(s[i] == c)
        {
            if(ret == s.size())
            {
                ret = i;
            }
            ++occurs;
        }
    }
    return ret;
}

int main()
{
    int co = 1;
    count(co);
    count(co);
    count(co);
    cout << "arge in main:" << co << endl;
    string str("Then one day the mother eagle appeared at the top of the mountain");
    string::size_type countChar = 0;
    char f = 'o';
    auto index = find_char(str, f, countChar);
    cout << "char " << f << " first index is " << index << " ,count is "<< countChar << endl;
    return 0;
}

注释是个必要的东西,这里我采用了doxgen的一种注释形式,对函数进行了注解。

这样注释的好处是可以使用doxgen生成说明文档。

 

转载于:https://www.cnblogs.com/ChattyKu/p/9572725.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值