C++ //练习 13.17 分别编写前三题中所描述的numbered和f,验证你是否正确预测了输出结果。

C++ Primer(第5版) 练习 13.17

练习 13.17 分别编写前三题中所描述的numbered和f,验证你是否正确预测了输出结果。

环境:Linux Ubuntu(云服务器)
工具:vim
代码块
cpp 复制代码
/*************************************************************************
	> File Name: ex13.17.cpp
	> Author: 
	> Mail: 
	> Created Time: Tue 23 Apr 2024 09:48:13 AM CST
 ************************************************************************/

#include<iostream>
using namespace std;

static int nextsn = 1;

class numbered {
public:
    int mysn;

    numbered() : mysn(nextsn++) { } // Unique serial number for each object

    numbered(const numbered &n) : mysn(nextsn++) { } // Copy creates a new unique id

    numbered &operator= (const numbered &n) {
        mysn = nextsn++; // Ensure unique id on assignment
        return *this;
    }
};

void f1(numbered s) {
    cout << s.mysn << endl;
}

void f2(const numbered &s) {
    cout << s.mysn << endl;
}

int main() {
    numbered a, b = a, c = b;

    f1(a); f1(b); f1(c);
    f2(a); f2(b); f2(c);

    return 0;
}
运行结果显示如下
相关推荐
Dev7z7 分钟前
基于MATLAB图像处理的苹果品质自动分级系统设计与实现
开发语言·图像处理·matlab
源代码•宸12 分钟前
Golang基础语法(go语言指针、go语言方法、go语言接口、go语言断言)
开发语言·经验分享·后端·golang·接口·指针·方法
Bony-12 分钟前
Golang 常用工具
开发语言·后端·golang
Paul_092013 分钟前
golang编程题
开发语言·算法·golang
csbysj202013 分钟前
Go 语言变量作用域
开发语言
牛奔16 分钟前
GVM:Go 版本管理器安装与使用指南
开发语言·后端·golang
颜酱18 分钟前
用填充表格法-继续吃透完全背包及其变形
前端·后端·算法
百***787518 分钟前
2026 优化版 GPT-5.2 国内稳定调用指南:API 中转实操与成本优化
开发语言·人工智能·python
夏秃然21 分钟前
打破预测与决策的孤岛:如何构建“能源垂类大模型”?
算法·ai·大模型
ChoSeitaku25 分钟前
16.C++入门:list|手撕list|反向迭代器|与vector对比
c++·windows·list