⚠️ 一个只能看,一个能动

👁️ 1 人浏览 💬 0 人评论 ❤️ 添加收藏

(本条路线统一用 g++ -std=c++17 -O0 -Wall 编译)一个只读的参数,一个可写的参数:

#include <iostream>
#include <string>

/* const& 收进来的东西改不了,编译器会替你把关 */
int length_of(const std::string &s) { return static_cast<int>(s.size()); }

void shout(std::string &s) { s += "!"; }      /* 非 const 引用才改得动 */

int main() {
    std::string a = "hello";
    int n = length_of(a);
    shout(a);
    std::cout << n << "/" << a << "/" << a.size() << "\n";
    return 0;
}
提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论