1

I have a base class

class Base{
public:
virtual ~Base();

};

I derive two classes from Base:

class D1:public Base{
//...some fields
//assignment operator, it does the deep copy of the members
D1& operator=(const D1&);
};

class D2:public Base{
//...some fields
//assignment operator, it does the deep copy of the members
D2& operator=(const D2&);
};

Next, in main I have two objects of let's say D1. The problem is that the overriden assignment operator is never called, however default one for base is called. I tried to make assignment operator virtual in Base, but it didn't help.

D1 *d1 = new D1();
D1 *d1_another = new D1();
//this doesn't work:
d1 = d1_another

D2 *d2 = new D2();
D2 *d2_another = new D2();
//this doesn't work:
d2 = d2_another

UPD Also I would like to know how to deal with

Base *d1 = new D1();
Base *d1_another = new D1();
//?
d1 = d1_another
Pavlo Kovalov
  • 91
  • 1
  • 10
  • 7
    you assign the pointers... [this way](http://coliru.stacked-crooked.com/a/728183b3c839551e) – user1810087 Feb 01 '19 at 10:39
  • @user1810087 `*d1 = *d1_another`? – Pavlo Kovalov Feb 01 '19 at 10:42
  • @PavloKovalov: Try it and see. – P.W Feb 01 '19 at 10:43
  • exactly, see link :) – user1810087 Feb 01 '19 at 10:43
  • @user1810087 tnank you! – Pavlo Kovalov Feb 01 '19 at 10:50
  • @user1810087, but what to do if I need to assign `Base* d1 = new D1()` to `Base* d1_another = new D1()`? if I write `*d1 = *d1_another` assignment operator prints nothing – Pavlo Kovalov Feb 01 '19 at 10:57
  • Base and D1 are distict types in this case and the compiler does not know how to assign them, since D1 can have additional "fields". You need to define your own assginments for **all** types you want to cover. [like this](http://coliru.stacked-crooked.com/a/9946c26f371f6fc5). see [here](https://stackoverflow.com/a/669894/1810087) a good discussion. – user1810087 Feb 01 '19 at 11:17
  • 1
    Assignment and derived classes don't play well togetger. Combining them is a design error more often than not. You hovever are never assugning any base or derived objects. You are only assigning pointers. – n. m. could be an AI Feb 01 '19 at 11:36

1 Answers1

1

Try it like this

#include <iostream>
#include <string>

using namespace std;

class Base {
    public:
    virtual ~Base() {}

};


class D1 : public Base {
public:
    virtual ~D1() {}
    //...some fields
    //assignment operator, it does the deep copy of the members
    D1& operator=(const D1&) {
        cout << "D1:operator=(const D1&)\n";
        return *this;
    }
};

class D2 : public Base {
public:
    virtual ~D2() {}
    //...some fields
    //assignment operator, it does the deep copy of the members
    D2& operator=(const D2&) {
        cout << "D2:operator=(const D2&)\n";
        return *this;
    }
};

main

    D1 *d1 = new D1();
    D1 *d1_another = new D1();
    //this doesn't work:
    *d1 = *d1_another;

    D2 *d2 = new D2();
    D2 *d2_another = new D2();
    //this doesn't work:
    *d2 = *d2_another;
Hamza.S
  • 1,319
  • 9
  • 18