Ashot
Ashot

Reputation: 10979

Check if copy constructor is called

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?

Upvotes: 9

Views: 1895

Answers (1)

Simple
Simple

Reputation: 14400

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...
};

Upvotes: 10

Related Questions