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;
}
运行结果显示如下
相关推荐
haibindev16 分钟前
别让AI再从零写一堆优美的屎山了
c++·ai编程·claude·流媒体·codex·代码复用
Zhang~Ling18 分钟前
C++ 模板初阶:从函数模板到类模板
c++
Promising_GEO29 分钟前
全球综合评估模型-GCAM模型的安装与参数解读
开发语言·python·遥感·空间分析
蜕变的土豆29 分钟前
Visual Studio编译时,报错windows sdk 不匹配,找不到windows sdk
c++
XS03010633 分钟前
并发编程二
java·开发语言
雪度娃娃34 分钟前
转向现代C++——优先选用限定作用域的枚举型别,而非不限作用域的枚举型别
java·jvm·c++
咩咦34 分钟前
C++学习笔记17:析构函数
c++·学习笔记·类和对象·构造函数·析构函数·动态内存
爱炸薯条的小朋友38 分钟前
全局锁的性能优势,以及链路优化为何常常低于预期——基于 `MatPoolsTest` 中小图池与大图池的实战复盘
opencv·算法·c#