#ifndef NOISY_H #define NOISY_H #include // Noisy class that reports when it is being created, copied, assigned // or destroyed. // // Copyright (c) 2005 by Curtis Krauskopf // http://www.decompile.com // Permission to use, copy, modify, distribute and sell this software for any // purpose is hereby granted without fee, provided that the above copyright // notice appears in all copies of this file and in all modified versions // of this file. // The author makes no representations about the suitability of this // software for any purpose. It is provided "as is" without express // or implied warranty. // Created: March 26, 2005 by Curtis Krauskopf (cdk) // http://www.decompile.com // // // Noisy is debugging tool. When added to a class or struct, it // reports when the data elements of the class or struct are automatically // being created, copied, assigned or destroyed. This is a non-intrusive // way of being able to detect those situations. In the below example, // a Payload struct contains a std::string. // // This technique is designed to detect automatic copying and assignment. // // This technique fails when the struct or class defines its own // copy constructor or assignment operator unless that copy constructor // or assignment operator also copy or assign the Noisy member. // // Example: // // struct Payload { // std::string name; // Noisy x; // the Noisy variable name doesn't matter. // }; // // ... // { // Payload p1; // emits "Noisy c'tor" // Payload p2 = p1; // emits "Noisy copy" // } // emits "Noisy destructor" twice class Noisy { public: Noisy() { std::cout << "Noisy c'tor" << std::endl; } Noisy(const Noisy &) { std::cout << "Noisy copy" << std::endl; // Nothing to copy } ~Noisy() { std::cout << "Noisy destructor" << std::endl; } Noisy & operator=(const Noisy &rhs) { std::cout << "Noisy =" << std::endl; if (this != &rhs) { // Nothing to do } return *this; } }; #endif