I have a existed class and function which look like this:
Class base_class{
...
}
void Func(...,vector &vec_b,...){
// inside
This only works if you're using pointers.
If you're not using pointers, then there is a chance for slicing to occur. Eg.
class Base {
protected:
int foo;
};
class Child : public Base {
int bar;
};
The class Base
contains only a single int called foo
, and the class Child
contains two ints, foo
and bar
.
Child f;
Base sliced = (Child)f;
This will slice the child, causing it to remove some of the data from the object. You will not be able to cast the parent back into the child class, not unless your using pointers.
So, if you changed your vector<>
to have pointers instead of instances of the class then you can cast back and forth between parent/child.
Using something like:
vector vec;
vec[0] = static_cast (new Child());
...
Func(vec);
...
Would allow you to cast the members of your vector into their child classes.