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;
}
运行结果显示如下
相关推荐
FL16238631293 小时前
[C#][winform]基于yolov8的水表读数检测与识别系统C#源码+onnx模型+评估指标曲线+精美GUI界面
开发语言·yolo·c#
小白菜又菜5 小时前
Leetcode 3432. Count Partitions with Even Sum Difference
算法·leetcode
cnxy1885 小时前
围棋对弈Python程序开发完整指南:步骤1 - 棋盘基础框架搭建
开发语言·python
wuhen_n6 小时前
LeetCode -- 15. 三数之和(中等)
前端·javascript·算法·leetcode
sin_hielo6 小时前
leetcode 2483
数据结构·算法·leetcode
程序员-周李斌7 小时前
Java 死锁
java·开发语言·后端
Xの哲學7 小时前
Linux多级时间轮:高精度定时器的艺术与科学
linux·服务器·网络·算法·边缘计算
大头流矢7 小时前
归并排序与计数排序详解
数据结构·算法·排序算法
阿闽ooo7 小时前
外观模式:从家庭电源控制看“简化接口“的设计智慧
c++·设计模式·外观模式
油泼辣子多加7 小时前
【信创】算法开发适配
人工智能·深度学习·算法·机器学习