How can I check if my constructor or destructor were ever called? Thanks in advance for any idea, I'm learning C++.
Because of implicit calls I don't know if they were actually called.
How can I check if my constructor or destructor were ever called? Thanks in advance for any idea, I'm learning C++.
Because of implicit calls I don't know if they were actually called.
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?
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; }
};
one of the possible way is to put properly labeled "std::cout"s with distinctive messages for each constructor and destructor. note: i have edited my previous answer.
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.