noisy.h
来自「很经典的书籍」· C头文件 代码 · 共 73 行
H
73 行
//: C07:Noisy.h
// A class to track various object activities
#ifndef NOISY_H
#define NOISY_H
#include <iostream>
using std::endl;
class Noisy {
static long create, assign, copycons, destroy;
long id;
public:
Noisy() : id(create++) {
std::cout << "d[" << id << "]" << std::endl;
}
Noisy(const Noisy& rv) : id(rv.id) {
std::cout << "c[" << id << "]" << std::endl;
copycons++;
}
Noisy& operator=(const Noisy& rv) {
std::cout << "(" << id << ")=[" <<
rv.id << "]" << std::endl;
id = rv.id;
assign++;
return *this;
}
friend bool
operator<(const Noisy& lv, const Noisy& rv) {
return lv.id < rv.id;
}
friend bool
operator==(const Noisy& lv, const Noisy& rv) {
return lv.id == rv.id;
}
~Noisy() {
std::cout << "~[" << id << "]" << std::endl;
destroy++;
}
friend std::ostream&
operator<<(std::ostream& os, const Noisy& n) {
return os << n.id;
}
friend class NoisyReport;
};
struct NoisyGen {
Noisy operator()() { return Noisy(); }
};
// A Singleton. Will automatically report the
// statistics as the program terminates:
class NoisyReport {
static NoisyReport nr;
NoisyReport() {} // Private constructor
public:
~NoisyReport() {
std::cout << "\n-------------------\n"
<< "Noisy creations: " << Noisy::create
<< "\nCopy-Constructions: "
<< Noisy::copycons
<< "\nAssignments: " << Noisy::assign
<< "\nDestructions: " << Noisy::destroy
<< std::endl;
}
};
// Because of the following definitions, this file
// can only be used in simple test situations. Move
// them to a .cpp file for more complex programs:
long Noisy::create = 0, Noisy::assign = 0,
Noisy::copycons = 0, Noisy::destroy = 0;
NoisyReport NoisyReport::nr;
#endif // NOISY_H ///:~
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?