Reputation: 7843
How can I check if my constructor or destructor were ever called?
Because of implicit calls I don't know if they were actually called.
Upvotes: 2
Views: 266
Reputation: 1697
One of the possible ways is to put properly labeled "std::cout"s with distinctive messages for each constructor and destructor.
Upvotes: 0
Reputation: 54128
If you want to avoid or better manage implicit calls, you can qualify the constructor as explicit
. This will show you any implicit calls you might be missing at compile-time.
Once you've sorted out this usage, you can remove the qualification or leave in as you prefer.
If you want to track counts of ctor vs dtor calls, you could add a static counter for calls to each in the class and then use interlocked increment and decrement ops to count ctors and dtors respectively. This should show whether you are matching them up properly. You'd have to include copy ctor and any non-default ctors you have implemented in the class for this to work.
Upvotes: 1
Reputation: 15154
You can set a breakpoint to them and see if it gets hit. Or you can output a line to console:
class MyClass {
MyClass() { std::cout << "In constructor" << std::endl; }
~MyClass() { std::cout << "In destructor" << std::endl; }
};
Upvotes: 0
Reputation: 16081
Upvotes: 0
Reputation: 11814
Upvotes: 9
Reputation: 36082
Why don't you just put in a couple of cout << "I'm here" in your ctor/dtor or use the debugger and set break points there?
Upvotes: 6