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;
}
运行结果显示如下
相关推荐
小陈工31 分钟前
Python Web开发入门(十七):Vue.js与Python后端集成——让前后端真正“握手言和“
开发语言·前端·javascript·数据库·vue.js·人工智能·python
海清河晏11136 分钟前
数据结构 | 单循环链表
数据结构·算法·链表
H Journey36 分钟前
C++之 CMake、CMakeLists.txt、Makefile
开发语言·c++·makefile·cmake
wuweijianlove5 小时前
算法性能的渐近与非渐近行为对比的技术4
算法
研究点啥好呢5 小时前
Github热门项目推荐 | 创建你的像素风格!
c++·python·node.js·github·开源软件
_dindong5 小时前
cf1091div2 C.Grid Covering(数论)
c++·算法
AI成长日志5 小时前
【Agentic RL】1.1 什么是Agentic RL:从传统RL到智能体学习
人工智能·学习·算法
lly2024065 小时前
C 标准库 - `<stdio.h>`
开发语言
沫璃染墨5 小时前
C++ string 从入门到精通:构造、迭代器、容量接口全解析
c语言·开发语言·c++
jwn9995 小时前
Laravel6.x核心特性全解析
开发语言·php·laravel