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;
}
运行结果显示如下
相关推荐
saltymilk9 小时前
C++ 模板参数推导问题小记(模板类的模板构造函数)
c++·模板元编程
感哥9 小时前
C++ lambda 匿名函数
c++
沐怡旸15 小时前
【底层机制】std::unique_ptr 解决的痛点?是什么?如何实现?怎么正确使用?
c++·面试
感哥16 小时前
C++ 内存管理
c++
聚客AI16 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
大怪v18 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
惯导马工20 小时前
【论文导读】ORB-SLAM3:An Accurate Open-Source Library for Visual, Visual-Inertial and
深度学习·算法
骑自行车的码农1 天前
【React用到的一些算法】游标和栈
算法·react.js
博笙困了1 天前
AcWing学习——双指针算法
c++·算法
感哥1 天前
C++ 指针和引用
c++