Let's say I have the definitions
struct A{
int a1 = 0;
int a2 = 1;
};
struct B: public A{
int b1 = 2;
int b2 = 3;
};
And I have a B object that I'm going to call foo
, and I want to update its fields inherited from A to the values in another A object that I call bar
. Obviously I could just do this by assigning each field, but could object slicing be used here instead?
I tried using this code
(A) foo = bar;
but it doesn't modify the fields, which I'm guessing is because I'm somehow making a copy. So can I use object slicing to do this, and if so is it 1.) not UB and 2.) reasonable?