关于C++函数的参数及其传递方式的思考

C++的函数参数传递方式

C语言函数参数的传递方式只有传值一种,又分为传变量值和传变量地址值两种情况,而C++的函数参数传递方式有两种:第一种和C语言一样是传值,第二种是传引用,即传对象的地址,所以也称传地址方式。

注意传地址值传递的是值,是以对象指针作为参数;而传地址传递的是地址是以对象引用作为参数

所以在设计函数参数时,可以使用“对象”、“对象指针和“对象引用'作为参数

对象作为函数参数

使用对象作为函数参数,是将实参对象的值传递给形参对象,传递是单向的形参具有实参的备份,当在函数中改变形参的值时改变的是这个备份中的值,不影响原来实参的值

#include <iostream>
#include <string>
using namespace std;
void swap(string, string);
int main()
{
    string str1 = "现在", str2 = "过去";
    swap(str1, str2);
    cout << "返回后{"
         << "str1:" << str1 << ",str2:" << str2 << "}" << endl;
    return 0;
}
void swap(string s1, string s2)
{
    string temp = s1;
    s1 = s2;
    s2 = temp;

    cout << "函数体内{"
         << "str1:" << s1 << ",str2:" << s2 << "}" << endl;
}

函数体内{str1:过去,str2:现在}

返回后{str1:现在,str2:过去}

对象指针作为函数参数

使用指向对象的指针作为函数参数,形参是对象指针(指针可以指向对象的地址),实参可以是对象的地址值,虽然参数传递方式仍然是传值方式,但因为形参传递的就是实参本身,所以当在函数中改变形参的值时,改变的就是原来实参的值

传对象地址值要用到对象的指针,而对于数组,因数组名就是数组的指针名所以数组也能用传数组地址值的方式。

#include <iostream>
#include <string>
using namespace std;
void swap(string *, string *);
int main()
{
    string str1 = "现在", str2 = "过去";
    swap(&str1, &str2);
    cout << "返回后{"
         << "str1:" << str1 << ",str2:" << str2 << "}" << endl;
    return 0;
}
void swap(string *s1, string *s2)
{
    string temp = *s1;
    *s1 = *s2;
    *s2 = temp;
    cout << "函数体内{"
         << "str1:" << *s1 << ",str2:" << *s2 << "}" << endl;
}

函数体内{str1:过去,str2:现在}

返回后{str1:过去,str2:现在}

引用作为函数参数

使用引用作为函数参数,在函数调用时,实参对象名传给形参对象名,形参对象名就成为实参对象名的别名。实参对象和形参对象代表同一个对象,所以改变形参对象的值就是改变实参对象的值。

#include<iostream>
#include<string>
using namespace std;
void swap(string & ,string &);
int main()
{
    string str1 = "现在", str2 = "过去";
    swap(str1, str2);
    cout << "返回后{"
         << "str1:" << str1 << ",str2:" << str2 << "}" << endl;
    return 0;
}
void swap(string &s1, string &s2)
{
    string temp = s1;
    s1 = s2;
    s2 = temp;
    cout << "函数体内{"
         << "str1:" << s1 << ",str2:" << s2 << "}" << endl;
}

函数体内{str1:过去,str2:现在}

返回后{str1:过去,str2:现在}

默认参数

默认参数就是不要求程序员设定该参数,而由编译器在需要时给该参数赋默认值。当程序员需要传递特殊值时,必须显式的指明。默认参数必须在函数原型中说明,默认参数可以多于1个,但必须放在参数序列的后部。

#include <iostream>
#include <string>
using namespace std;
void Display(string s1, string s2 = "", string s3 = "");
int main()
{
    string str1("现在"), str2("过去"), str3("将来");
    Display(str1);
    Display(str1, str2, str3);
    Display(str3, str1);
    Display(str2, str3);
}
void Display(string s1, string s2, string s3)
{
    if (s2 == "" && s3 == "")
    {
        cout << s1 << endl;
    }
    else if (s3 == "" && s2 != "")
    {
        cout << s1 << "、" << s2 << endl;
    }
    else
    {
        cout << s1 << "、" << s2 << "、"
             << "s3" << endl;
    }
}

现在

现在、过去、s3

将来、现在

过去、将来

使用const保护数据

可以用cost修饰要传递的参数,意思是通知函数,它只能使用参数而无权修改参数,以提高系统的自身安全,C++中普遍使用这种方法。

#include <iostream>
#include <string>
using namespace std;
void change(const string &);
int main()
{
    string str("Can you change it?");
    change(str);
    cout << str << endl;
    return 0;
}
void change(const string &s)
{
    string s2 = s + "No!";
    cout << s2 << endl;
}

Can you change it?No!

Can you change it?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.huang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值