tags:

views:

159

answers:

4

I want to find the objects of a class that are currently alive in C++. Please tell me a solution for this. Also, please correct if my logic is wrong!

  • Declare a global variable.
  • Increment it during the constructor invocation.
  • Decrement during destructor invocation.

Thanks in advance.

Sanjeev

+5  A: 

keep a static variable in your class as count. Global variables are not good practice.

Samrat Patil
give reasons for down vote guys :)
Samrat Patil
I don't know why this was downvoted. +1 (counterbalance) .
Prasoon Saurav
+7  A: 

Your logic is correct except for one thing: don't make it a global variable; it is untidy and there is the danger that a bug in some other code might modify it. Instead make it a private static member variable of the class.

Beta
A: 

It's impossible in C++. Tracking constructor and destructor does not guarantee the accuracy: how do you prevent a memory copy like this:

memcpy(buf, &instance, sizeof(T));
T* anotherInstance = (T*)buf;
Codism
He may not being doing any of those crazy memcpy shenanigans in his program, so he wouldn't have to worry.
GWW
By this logic, you can't rely on any code to work properly, because someone could always go and write random garbage over your objects using memcpy.
PigBen
Defensive coding on the order of Core Wars...
Jon Purdy
@PigBen: That's the point, it's simply impossible in language like C++ in which memory can be directory manipulated. May be the original question can be rephrased to how to track constructor and destructor usage.
Codism
@Codism: No, by your logic, even that is impossible. Because whatever variable you are using to keep track of calls to the constructor could be overwritten by poking around in memory.
PigBen
@PigBen: I don't think it's a fair argument because poking around in memory is a bug while in C/C++, using a buffer declared in another type (usually char*) is quite common.
Codism
@Codism: You mean in C. Not in C++. I've certainly never written C++ code like that.
PigBen
@Codism: Let me clarify that. I've never used memcpy to copy class objects. If I did, it was for a brief intellectual curiosity. But never in production code. I used to use memcpy for copying long arrays of integers or other simple types like that, especially for copying bitmap data(not the whole object, just the pixel data). But I would never try to copy a class object like that.
PigBen
+1  A: 

Take a look at the curiously recurring template pattern, it is great for this sort of thing and the example in wikipedia shows how to use it for an object counter.

template <typename T>
struct counter
{
    counter()
    {
        ++objects_created;
        ++objects_alive;
    }

    virtual ~counter()
    {
        --objects_alive;
    }
    // if you are using multiple threads, these need to be protected
    // with interlocked operations as appropriate per your compiler + platform
    static int objects_created;
    static int objects_alive;
};
template <typename T> int counter<T>::objects_created( 0 );
template <typename T> int counter<T>::objects_alive( 0 );

class X : counter<X>
{
    // ...
};

class Y : counter<Y>
{
    // ...
};
Rick
Edited to format code properly. This is missing a copy constructor, though, and I'm not sure whether the destructor should be virtual - perhaps making it protected would be a better idea (perhaps the constructors should also be protected, to avoid increasing the count by creating counters!).
UncleBens
thanks, we should probably ping wiki too on this since I copied the example from there.In my own uses of this pattern the classes are usually non-copyable.
Rick