0

move derived obj d to derived dd, but match base's move constructor, do not match derived's constructor

// base class 
class base {
    int x;

public:
    base() = default;

    base(base && b) {
        x = b.x;
    }
};

// derived struct
struct derived : public base {
    int y;

    derived(int d) {
        y = d;
    }

    derived() = default;
};

int main()
{
    derived d{1};
    derived dd = std::move(d);
    spdlog::info(dd.y); // => 1
    return 0;
}

I do not know why dd.y will be 1, not 0 ?

how d.y copied to dd.y struct ?

langyu
  • 68
  • 1
  • 7

0 Answers0