1
votes

Im receiving error C2082: redefinition of formal parameter 'rval' in this code while trying to call base copy ctor explicitly:

 #include <iostream>
 using namespace std;


class Base
{
public:
    Base(const Base& rhs){ cout << "base copy ctor" << endl; }
};

class Derived : public Base
{
public:
    Derived(const Derived& rval) { Base(rval) ; cout << "derived copy ctor" << endl; }
      // error C2082: redefinition of formal parameter 'rval'
};

int main()
{
    Derived a;
    Derived y = a; // invoke copy ctor
    cin.ignore();
    return 0;
}

However if do it like this:

Derived(const Derived& rval) { Base::Base(rval) ; cout << "derived copy ctor" << endl; }

then is OK.

Why am I asking this? according to the answers on StackOwerflow

I do not have to use operator :: to access base copy ctor, so why do I receive this error?

btw: I'm using visual studio 2010.

I'm having one more question:

Do I have to call base's move constructor in user defined move constructor of derived class?

2

2 Answers

2
votes

To call the base constructor you need to put the call in the member initalization list

class Derived : public Base
{
public:
    Derived(const Derived& rval) : Base(rval)
    { 
         cout << "derived copy ctor" << endl;
    }
};
1
votes

Assuming that you mean 'move' constructor is the copy constructor - Yes. You will have to call the Base's constructor. Otherwise the definition if the base object within the derived object will not be complete. You can either call a copy constructor or a normal constructor of the base class.