Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 988
Ashot Avatar asked Nov 02 '13 15:11

Ashot


People also ask

When a copy constructor is called?

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.

When a copy constructor is not called?

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.

How many times copy constructor is called?

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.


1 Answers

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...
};
like image 126
Simple Avatar answered Oct 15 '22 08:10

Simple