[英]I'm receiving error when calling base copy constructor
Im receiving error C2082: redefinition of formal parameter 'rval'
in this code while trying to call base copy ctor explicitly: 我收到
error C2082: redefinition of formal parameter 'rval'
尝试显式调用基本副本ctor时,在此代码中error C2082: redefinition of formal parameter 'rval'
:
#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
根据StackOwerflow上的答案
I do not have to use operator ::
to access base copy ctor, so why do I receive this error? 我不必使用operator
::
来访问基本副本ctor,那么为什么会收到此错误?
btw: I'm using visual studio 2010. 顺便说一句:我正在使用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? 我是否必须在派生类的用户定义的移动构造函数中调用base的移动构造函数?
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;
}
};
Assuming that you mean 'move' constructor is the copy constructor - Yes. 假设您的意思是“移动”构造函数是副本构造函数-是的。 You will have to call the Base's constructor.
您将必须调用Base的构造函数。 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.
您可以调用基类的副本构造函数或普通构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.