I am debugging a program. I need to know if copy constructor is called for some class. Since I haven't defined copy constructor the compiler has generated it. I tried to define it and put some cout
there but now I must write the copying part manually. The class is huge so I don't want to do it. Is there an way to check if copy constructor called but avoid writing the copying of its members. How can I call the default implementation of copy constructor?
A copy constructor is called when a new object is created from an existing object, as a copy of the existing object. The assignment operator is called when an already initialized object is assigned a new value from another existing object.
The reason the copy constructor is not called is because the copy constructor itself is a function with one parameter. You didn't call such function,so it didn't execute.
And there are 4 calls to copy constructor in f function. 1) u is passed by value. 2) v is copy-initialized from u . 3) w is copy-initialized from v . 4) w is copied on return.
You can use a mixin:
template<class D>
struct traced
{
public:
traced() = default;
traced(traced const&) { std::cout << typeid(D).name() << " copy ctor\n"; }
protected:
~traced() = default;
};
And then you just inherit from the class like so:
class my_class : public traced<my_class>
{
// usual stuff...
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With