新用户登入 登录
萌子岛 返回首页

低手的个人空间 https://www.dommdo.com/?1097067 [收藏] [复制] [RSS]

日志

Passing a const

已有 71 次阅读2012-10-31 16:00

#include <iostream>
using std::cout;
using std::endl;
int incr10(const int& num); // Function prototype
int main(void)
{
const int num = 3; // Declared const to test for temporary creation
int value = 6;
int result = incr10(num);
cout << endl
<< “incr10(num) = “ << result;
cout << endl
<< “num = “ << num;
result = incr10(value);
cout << endl
<< “incr10(value) = “ << result;
cout << endl
<< “value = “ << value;
cout << endl;
return 0;
}
// Function to increment a variable by 10
int incr10(const int& num) // Function with const reference argument
{
cout << endl
<< “Value received = “ << num;
// num += 10; // this statement would now be illegal
return num+10; // Return the incremented value
}
The output when you execute this is:
Value received = 3
incr10(num) = 13
num = 3
Value received = 6
incr10(value) = 16
value = 6
How It

路过

雷人

握手

鲜花

鸡蛋

全部作者的其他最新日志

评论 (0 个评论)

小黑屋|萌子岛

GMT+8, 2025-5-3 04:32 , Processed in 0.323936 second(s), 18 queries , Gzip On, MemCache On.

Copyright 2020  atollmoe©.a2.1.0 All rights reserved. 9+

Copyright 2009  supported by zhixuan© oeo© oko© All rights reserved.Thank you!

返回顶部