J
Jon Slaughter
I'm having a little trouble understanding what the slicing problem is.
In B.S.'s C++ PL3rdEd he says
"Becayse the Employee copy functions do not know anything about Managers,
only the Employee part of Manager is copied. "....
and gives the code above as
.....
Employee e = m;
e = ml
I fail to see how this is a problem as we are "casting" m into e and
obviously Employee shouldn't copy anything of m into e because that is not
already in e itself(where would it go?)?
I was thinking that "slicing" refered to an object being "sliced" in half by
not being copyied all the way so it would be seem that if we did something
like
Manager m = e;
then we would have "sliced" m in half by only assigning those elements of m
that are also of e?
This seems strange to me though.
By analogy if a derived object is larger than its base and we are casting
from a derived object into its base then we are "slicing" off the parts of
the derived object that don't fit into the base? But isn't that the whole
point? Else we wouldn't do the code to do the slice in the first place?
Maybe I'm making it into a problem when its not? It just mentions that it
can be a source of suprices and errors but I don't see how unless someone
isn't really thinking about what is happening.
If I do something like:
Derived D;
Base B = D;
then potentially I see how that Derived could change a value that I normally
except Base wouldn't not expect to see.
struct Base
{
int data;
Base(int i) : data(i) {};
};
struct Derived : public Base
{
int new_data;
Derived(int i) : Base(i+1), new_data(4) {};
};
void main()
{
Derived D(1);
Base B = D;
return;
}
then, lets suppose that Base's data is never suppose to be > 1...
but B has B.data = 2 after the assignment and hence hence there is an error.
Is that the basic idea?
Thanks,
Jon
In B.S.'s C++ PL3rdEd he says
"Becayse the Employee copy functions do not know anything about Managers,
only the Employee part of Manager is copied. "....
and gives the code above as
.....
Employee e = m;
e = ml
I fail to see how this is a problem as we are "casting" m into e and
obviously Employee shouldn't copy anything of m into e because that is not
already in e itself(where would it go?)?
I was thinking that "slicing" refered to an object being "sliced" in half by
not being copyied all the way so it would be seem that if we did something
like
Manager m = e;
then we would have "sliced" m in half by only assigning those elements of m
that are also of e?
This seems strange to me though.
By analogy if a derived object is larger than its base and we are casting
from a derived object into its base then we are "slicing" off the parts of
the derived object that don't fit into the base? But isn't that the whole
point? Else we wouldn't do the code to do the slice in the first place?
Maybe I'm making it into a problem when its not? It just mentions that it
can be a source of suprices and errors but I don't see how unless someone
isn't really thinking about what is happening.
If I do something like:
Derived D;
Base B = D;
then potentially I see how that Derived could change a value that I normally
except Base wouldn't not expect to see.
struct Base
{
int data;
Base(int i) : data(i) {};
};
struct Derived : public Base
{
int new_data;
Derived(int i) : Base(i+1), new_data(4) {};
};
void main()
{
Derived D(1);
Base B = D;
return;
}
then, lets suppose that Base's data is never suppose to be > 1...
but B has B.data = 2 after the assignment and hence hence there is an error.
Is that the basic idea?
Thanks,
Jon