An easy way to detect construction, destruction, copy
and assignment
by Curtis Krauskopf
Q: How can I easily detect when the data elements
of a struct or class are being copied or assigned?
A: #include the following noisy.h
file in your project. As shown in the example, add a
Noisy data member to your class or struct. A message
will be sent to cout when the data element is being
constructed, destructed, copied or assigned.
Download
noisy.h (2.2k):
#ifndef NOISY_H
#define NOISY_H
#include <iostream>
// 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
|
|
|
|
|
Example Output:
Noisy c'tor Noisy copy Noisy destructor Noisy destructor
|
|
|
|
|
This
article was written by Curtis Krauskopf (email at ). Curtis Krauskopf is a software
engineer and the president of The Database Managers (www.decompile.com).
He has been writing code professionally for over 25 years. His prior projects
include multiple web e-commerce applications, decompilers
for the DataFlex language, aircraft simulators, an automated Y2K conversion
program for over 3,000,000 compiled DataFlex programs, and inventory control projects.
Curtis has spoken at many domestic and international DataFlex developer conferences
and has been published in FlexLines Online, JavaPro
Magazine, C/C++
Users Journal and C++ Builder Developer's Journal.
Popular C++ topics at The Database Managers:
The Database Managers
helps companies to:- become more profitable
- grow their
business
- fix programs
that are behaving badly
- write new programs
to solve business problems
- do more with
fewer resources
Email them at
to find out how to make your company more successful.
|
|