Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to "add" to the default copy constructor?

Is it possible to "add" to the default copy constructor?

Eg. For this class:

class A
{
    public:
        int a;
        int* b;
};

I want to just write

A::A(const A& rvalue):
    a(rvalue.a),
    b(new int(*(rvalue.b)))
{}

without the a(rvalue.a) part.

(Ignore the bad/ugly code and possible memory leaks)

like image 426
小太郎 Avatar asked Aug 04 '11 08:08

小太郎


1 Answers

What you ask for is impossible. Once you declare your own copy constructor the compiler will not generate a copy constructor for you. That means you won't be able to simply add or augment the default copy constructor because it won't exist. It's all or nothing, so to speak.

like image 114
In silico Avatar answered Oct 23 '22 08:10

In silico