简体   繁体   English

复制派生类的构造函数

[英]Copy Constructor for derived class

I have a Base class 我有一个基类

class Keyframebase
  {

    private:
std::string stdstrName;
float time;
KeyframeType keyframeType;

    public:
Keyframebase();
Keyframebase(KeyframeType keyType);
Keyframebase(const Keyframebase &key);
Keyframebase& operator = (const Keyframebase &key);
std::string getName();

  };

Which is derived by Another class. 这是由另一个类派生的。

   class SumKeyframeXYZ : public Keyframebase
      {
         private:
float x; 
float y;
float z;

          public:
SumKeyframeXYZ();
SumKeyframeXYZ(float x, float y, float z);
SumKeyframeXYZ(const SumKeyframeXYZ& key);
//  const Sum_Position& operator=(const Container& container);
SumKeyframeXYZ& operator=(const SumKeyframeXYZ& key);
void setValue(float x, float y, float z);  
  };

This is the copy constructor for the Derived class. 这是Derived类的复制构造函数。

SumKeyframeXYZ::SumKeyframeXYZ(const SumKeyframeXYZ& key) : Keyframebase( 
 key )
       {
        this->x = key.x;
        this->y = key.y;
        this->z = key.z;
       } 

Since i want to copy the Base class members also when i copy Object of derived class so is this the correct approach of giving a derived class object as argument to base class. 因为我想在复制派生类的Object时复制Base类成员,所以这是将派生类对象作为基类的参数给出的正确方法。

so is this the correct approach of giving a derived class object as argument to base class. 这是将派生类对象作为基类的参数给出的正确方法。

Correct. 正确。

SumKeyframeXYZ::SumKeyframeXYZ(const SumKeyframeXYZ& key)
   : Keyframebase( key )  ///<<< Call the base class copy constructor

is this the correct approach of giving a derived class object as argument to base class 这是将派生类对象作为基类的参数给出的正确方法

Yes, it is. 是的。

Or you can apply explicitly-defaulted function definition for this case, eg 或者您可以为此案例应用显式默认的函数定义 ,例如

SumKeyframeXYZ::SumKeyframeXYZ(const SumKeyframeXYZ& key) = default;

the compiler-generated copy constructor does the same thing as 编译器生成的复制构造函数与之相同

SumKeyframeXYZ::SumKeyframeXYZ(const SumKeyframeXYZ& key) : Keyframebase(key), 
                                                            x(key.x), 
                                                            y(key.y), 
                                                            z(key.z) 
{}

In a word, yes. 总之,是的。 The derived class should have to handle the logic of copying the base class' properties, but delegate that responsibility to the base class, as an act of proper encapsulation. 派生类应该必须处理复制基类属性的逻辑,但是将该责任委托给基类,作为适当封装的行为。

so is this the correct approach 这是正确的方法

It kind of is. 它有点像。 See also Item 12 in Effective C++ ("Copy all parts of an object"), where the author gives a quite similar example. 另请参阅Effective C ++中的第12项(“复制对象的所有部分”),其中作者给出了一个非常相似的示例。

However, note that it's usually best to use the compiler-generated default versions of special member functions if you can (assuming KeyframeType is copyable and copying an instance does the right thing). 但是,请注意,如果可以的话,通常最好使用编译器生成的特殊成员函数的默认版本(假设KeyframeType是可复制的,复制实例是正确的)。 In your case, it seems you can. 在你的情况下,似乎你可以。 Whenever member-wise copy of all data member is fine, simply going with 每当所有数据成员的成员明智副本都没问题时,只需使用即可

SumKeyframeXYZ(const SumKeyframeXYZ&) = default;

in your class definition is the way to go. 在你的班级定义中是要走的路。 You could also omit it, but not that the rule of five actually demands that you be explicit about the default ness of your special members, ie, all of them (and it kicks in as you have a virtual destructor in your base class). 你也可以省略它,但不是五条规则实际上要求你明确你的特殊成员的default ,即所有这些(当你在你的基类中有一个virtual析构函数时它就会开始)。

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

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