0

We are unable to copy derived class objects, when copy constructor of base class is private ,but when we write our own copy constructor in derived class , then we are able to copy object , Why?

#include <iostream>
class base
{
public:
    base()
    {
    }

private:
    base(const base &x)
    {
        std::cout << "copy constructor of base class";
    }
};
class derived : public base
{
public:
    derived(){};
    derived(const derived &X)             //If we remove this, we are not able to create copy? But why?
    {
        std::cout << "copy of derived class";
    }
};

int main()
{
    derived x;
    derived y(x);//valid with our own derived class copy constructor 
}

Just to understand this after reading comments and answer , I wrote another program

#include <iostream>
class base
{
protected:
    int x;
    int y;
public:
    base() {}
    base(int x, int y) : x(x), y(y)
    {
    }
};
class derived : public base
{
    int a;

public:
    derived()
    {
    }
    derived(int a, int x, int y) : a(a), base(x, y){};
    derived(derived const &x)
    {
        this->a = x.a;
    }
    void get()
    {
        std::cout << a;
        std::cout << "\t" << x << "\t" << y;
    }
};
int main()
{
    derived a(99, 2, 3);
    a.get();
    derived b(a);
    std::cout << std::endl;
    b.get();
}

This gave me output as

99      2       3
99      -2145089504     1

Also I found Why aren't copy constructors "chained" like default constructors and destructors? worth a read

user4581301
  • 33,082
  • 7
  • 33
  • 54
Lion's_Den
  • 121
  • 1
  • 7
  • 3
    Your constructor `derived(const derived &X)` by default calls `base()` which is public you have to explicitly call the base class copy constructor if you want it (which would fail as it is private). The compiler generated copy constructor of `derived` calls the copy constructor of `base` which is private which is why it fails before. – Martin York Mar 25 '21 at 18:35
  • @MartinYork what happens if I remove this and use complier's copy constructor ? – Lion's_Den Mar 25 '21 at 18:36
  • See above comment. – Martin York Mar 25 '21 at 18:37
  • @MartinYork Thanks Mate !! – Lion's_Den Mar 25 '21 at 18:59
  • In your second code block, the `get()` routine is doing *undefined behavior* when `x` and `y` are not initialized. – Eljay Mar 25 '21 at 21:24

2 Answers2

1

We are unable to copy derived class objects, when copy constructor of base class is private

The implicit copy constructor of the derived class copy initialises the base. If the copy constructor of base is private, then the derived class has no access to it and cannot copy the base. If you explicitly define a derived class copy constructor that doesn't copy the base, then there is no issue - other than the issue of the base being default initialised.

eerorika
  • 232,697
  • 12
  • 197
  • 326
-1

I think you are assuming that the copy constructor of derived will call the copy constructor of base but that's not the case , it calls the constructor . As said in comments you need to explicitly call copy constructor to get the default behaviour .

Moreover the default behavior is maintained by compiler by deleting the copy constructor of the derived class when the base class copy constructor is private .

so compiler will delete both copy constructor and assignment operator

  // derived(const derived &) = delete;
  // derived(derived &&) = delete;

In your case you explicitly declared the copy constructor so compiler never need not to explicitly delete them.

User
  • 572
  • 2
  • 10