简体   繁体   English

调用基本副本构造函数时收到错误

[英]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.

相关问题 在派生类中调用基类副本构造函数 - Calling Base Class Copy Constructor in Derived Class 编译错误:调用基本类型构造函数时转换无效 - Compilation error: invalid conversion when calling base-type constructor Clang-Tidy:调用复制构造函数以外的基本构造函数 - Clang-Tidy: Calling a base constructor other than the copy constructor 从派生复制构造函数调用基类的移动构造函数 - Calling base's move constructor from derived copy constructor 当Base和Derived都使用Derived类型参数进行模板化时,调用Base构造函数时发生编译器错误 - Compiler Error When Calling Base Constructor when both Base and Derived are Templated With Derived Type Parameter 为什么在调用基类复制构造函数(C ++)时将派生类类型作为参数传递? - Why passing derived class type as parameter when calling base class copy constructor (C++)? 调用基类的构造函数时隐式的“ this” - Implicit 'this' when calling base class' constructor 缺少默认构造函数 - 但我没有调用它? - Default constructor missing - but I'm not calling it? 在构造函数定义中调用函数后收到Segmentation Fault 11错误? - Receiving Segmentation Fault 11 error after calling function in a constructor definition? 在基类上复制构造函数 - copy constructor on base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM