gpt4 book ai didi

c++ - 派生类的复制构造函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:55:59 24 4
gpt4 key购买 nike

我有一个基类

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();

};

由另一个类派生。

   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);
};

这是派生类的复制构造函数。

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

因为我想在复制派生类的对象时也复制基类成员,所以这是将派生类对象作为基类参数的正确方法。

最佳答案

so is this the correct approach of giving a derived class object as argument to base class.

正确。

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

关于c++ - 派生类的复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57176738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com