我有一个基类。
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);
};
这是Derived类的复制构造函数。
SumKeyframeXYZ::SumKeyframeXYZ(const SumKeyframeXYZ& key) : Keyframebase(
key )
{
this->x = key.x;
this->y = key.y;
this->z = key.z;
}
由于我想在复制派生类对象时也复制基类成员,所以将派生类对象作为参数传递给基类是否是正确的方法。
this->x = key.x
等语句,并用适当的成员初始化习惯用法: Keyframebase(key), x(key.x)
进行替换。 - n. m.