I've written two files:
anyclass.cpp
anyclass.hpp
Within is the definition of the class "AnyClass". It's used for testing
purposes and for playing around, having some fun.
What the class does is (optionally) print text whenever an object of it is
created, destroyed, assigned to.
The constructor works as so:
AnyClass blah("blah");
You supply it with a name. This name will be copied into its internal
storage. All text which is printed will refer to the object by name. If you
do the following:
AnyClass poo = blah;
Then the object "poo" will have the name "Copy of blah".
Here's its public members:
object_counter : a static read-only variable of type "unsigned" which holds
the amount of objects of "AnyClass" currently in existence.
GetName() : returns a pointer to the string which contains the name of the
object.
to_play_with : just a non-const member variable of type "unsigned" to play
around with.
Here's an example usage:
#include "anyclass.hpp"
int main()
{
AnyClass const &blah = AnyClass("no-name");
std::cout << "\nHaHa\n";
}
This program will output the following:
AnyClass Constructor for: no-name
HaHa
AnyClass Destructor for: no-name
Which shows that the temporary in the above is valid until the end of the
function.
If you don't want the class to print out at all, then do the following:
#define ANYCLASS_NO_OUTPUT
#include "anyclass.hpp"
but unfortunately you'll also have to edit the ".cpp" file also to achieve
this.
Here it is. . .
//anyclass.hpp
#ifndef INCLUDE_ANYCLASS_HPP
#define INCLUDE_ANYCLASS_HPP
#ifndef ANYCLASS_NO_OUTPUT
#include <iostream>
#endif
#include <cstddef>
#include <cstring>
class AnyClass
{
public:
static unsigned const &object_counter;
unsigned to_play_with;
const char* GetName() const
{
return name;
}
private:
static unsigned object_counter_prv;
char* name;
public:
AnyClass(const char* const in_name, unsigned const in_to_play_with = 0)
: to_play_with(in_to_play_with)
{
std::size_t length = std::strlen( in_name );
name = new char[length += 1];
memcpy(name, in_name, length);
++object_counter_prv;
#ifndef ANYCLASS_NO_OUTPUT
std::cout << " AnyClass Constructor for: " << name <<
'\n';
#endif
}
AnyClass(AnyClass const &original) : to_play_with
(original.to_play_with)
{
std::size_t length = std::strlen( original.name );
name = new char[length += 9]; //9 = 8 + 1 (1 for the null
character)
memcpy( name, "Copy of " , 8 ); // . . .waste of a null
character
memcpy( &name[8], original.name, length -= 9 ); //Take the 9
back off
name[length += 8] = '\0';
++object_counter_prv;
#ifndef ANYCLASS_NO_OUTPUT
std::cout << "AnyClass Copy Constructor for: " << name << '\n';
#endif
}
AnyClass& operator=(AnyClass const &other)
{
//NB: There is no name change whatsoever
to_play_with = other.to_play_with;
#ifndef ANYCLASS_NO_OUTPUT
std::cout << " AnyClass Assignment Operator: " << name << "
= " << other.name << '\n';
#endif
return *this;
}
~AnyClass()
{
#ifndef ANYCLASS_NO_OUTPUT
std::cout << " AnyClass Destructor for: " << name <<
'\n';
#endif
delete [] name;
--object_counter_prv;
}
};
#endif
//anyclass.cpp
#include "anyclass.hpp"
unsigned AnyClass::object_counter_prv = 0;
unsigned const &AnyClass::object_counter = AnyClass::object_counter_prv;
Any comments, questions, suggestions welcomed.
-JKop