简体   繁体   English

在派生类构造函数中复制继承的成员是否有效?

[英]Is it valid to copy an inherited member in the derived class constructor?

In the below code I have defined an explicit copy constructor in the derived class. 在下面的代码中,我在派生类中定义了一个显式副本构造函数。 I have also written my own copy constructor in the base class. 我还在基类中编写了自己的副本构造函数。

Primer says that the derived copy constructor must call the base one explicitly and that inherited members should be copied by the base class copy constructor only, but I copied the inherited members in the derived class copy constructor and it is working fine. Primer说派生副本构造函数必须显式调用基类,并且继承成员只能由基类副本构造函数复制,但是我在派生类副本构造函数中复制了继承成员,并且工作正常。 How is this possible? 这怎么可能?

Also how do I explicitly call the base class copy constructor inside the derived class copy constructor? 另外,如何在派生类副本构造函数内部显式调用基类副本构造函数? Primer says base(object), but I am confused how the syntax differentiates between normal constructor call and copy constructor call. Primer说base(object),但是我对语法如何区分普通构造函数调用和复制构造函数调用感到困惑。

Thanks in advance. 提前致谢。

#include<stdafx.h>
#include<iostream>

using namespace std;

class A
{
public:
  int a;
  A()
  {
    a = 7;
  }

  A(int m): a(m)
  {
  }
};

class B : public A
{
public:
  int b;
  B()
  {
    b = 9;
  }

  B(int m, int n): A(m), b(n)
  {
  }

  B(B& x)
  {
    a = x.a;
    b = x.b;
  }

  void show()
  {
    cout << a << "\t" << b << endl;
  }
};

int main()
{
  B x;
  x = B(50, 100);
  B y(x);
  y.show();
  return 0;
}

copy constructor is when you have another Object passed to your constructor: 复制构造函数是在将另一个Object传递给构造函数时:

class A() {
    private:
        int a;
    public:
        //this is an empty constructor (or default constructor)
        A() : a(0) {};
        //this is a constructor with parameters
        A(const int& anotherInt) : a(anotherInt) {};
        //this is a copy constructor
        A(const A& anotherObj) : a(anotherObj.a) {}; 
}

for a derived class 对于派生类

class B : public A {
    private:
         int b;
    public:
         //this is the default constructor
         B() : A(), b() {};
         //equivalent to this one
         B() {};
         //this is a constructor with parameters
         // note that A is initialized through the class A.
         B(const int& pa, const int& pb) : A(pa), b(pb) {}
         //for the copy constructor
         B(const B& ob) : A(ob), b(ob.b) {}        
}

How somebody else wrote here: 别人怎么写在这里:

How to call base class copy constructor from a derived class copy constructor? 如何从派生类副本构造函数调用基类副本构造函数?

I would write the copy constructor like this instead how it was written by macmac: 我会像这样编写副本构造函数,而不是由macmac编写的:

B(const B& x) : A(x) , b(x.b)
{
}

To call the copy constructor of the base A you simply call it passing the derived B object A(B), is not neccessary to specify Ba 要调用基础A的副本构造函数,只需将其传递给派生的B对象A(B)即可,无需指定Ba

EDIT: macmac edited his answere in the correct way, now his answere is better then mine. 编辑:macmac以正确的方式编辑了他的答覆,现在他的答覆比我的好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM