S
subramanian100in
Consider
class Base
{
....
};
class Derived : public Base
{
...
};
Derived d_obj;
As per my understanding, slicing happens in the following scenarios
and they should be avoided. However my question is, will we ever
require one of the following scenarios?
1) Will there be any need to instantiate Base like
Base b_obj(d_obj); // derived object sliced down to base object
2) Will there be any need to assign like
b_obj = d_obj; // again derived object sliced down to base object
3) void fn(Base base_obj_arg)
{
...
}
fn(d_obj); // slicing happens here
4) Base fn(const Derived& arg)
{
return arg;
}
Base temp = fn(d_obj); // again slicing happens here.
What I am trying to say is, if the above four scenarios are avoided,
slicing will not happen. However will there be any need for one of the
above four scenarios to be used as mandatory ?
Kindly explain.
Thanks
V.Subramanian
class Base
{
....
};
class Derived : public Base
{
...
};
Derived d_obj;
As per my understanding, slicing happens in the following scenarios
and they should be avoided. However my question is, will we ever
require one of the following scenarios?
1) Will there be any need to instantiate Base like
Base b_obj(d_obj); // derived object sliced down to base object
2) Will there be any need to assign like
b_obj = d_obj; // again derived object sliced down to base object
3) void fn(Base base_obj_arg)
{
...
}
fn(d_obj); // slicing happens here
4) Base fn(const Derived& arg)
{
return arg;
}
Base temp = fn(d_obj); // again slicing happens here.
What I am trying to say is, if the above four scenarios are avoided,
slicing will not happen. However will there be any need for one of the
above four scenarios to be used as mandatory ?
Kindly explain.
Thanks
V.Subramanian