Copy Constructor for derived class
Asked Answered
R

4

5

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.

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.

Reena answered 24/7, 2019 at 6:41 Comment(1)
You should also remove this->x = key.x etc and replace it with a proper member initialisation idiom : Keyframebase(key), x(key.x) etc`.Jacksmelt
D
2

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.

Delightful answered 24/7, 2019 at 6:45 Comment(0)
N
4

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
Niue answered 24/7, 2019 at 6:45 Comment(0)
R
4

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, e.g.

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) 
{}
Retool answered 24/7, 2019 at 6:49 Comment(0)
D
2

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.

Delightful answered 24/7, 2019 at 6:45 Comment(0)
K
2

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.

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). 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 defaultness of your special members, i.e., all of them (and it kicks in as you have a virtual destructor in your base class).

Kidney answered 24/7, 2019 at 6:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.