The Background:
I'm building a physics engine in C++ that computes the gravitational evolution of an n-body system in Cartesian space and then translates that into any of a predefined set of coordinate systems. Eventually the goal is the make the starting coordinate system arbitrary (calculate in coordinate system 'n' instead of only Cartesian), but that is a distant goal.
The Problem:
Because the coordinate system is supposed to be interchangeable, I have made the Cartesian coordinate system extend a base coordinate system:
class CoordMember {
}
class CoordState {
public:
/* methods to operate on members */
protected:
std::vector<CoordMember*> members;
}
class Particle : public CoordMember {
}
class CartState : public CoordState {
}
The error arises when trying to create a pointer of type std::vector<Particle*>
which points to the members object of type std::vector<CoordMember*>
:
CartState* state = new CartState(/* initialization vars */);
std::vector<Particle*>* parts = static_cast< std::vector<Particle*>* >(&state->members);
Compiler errors are:
error: static_cast from 'std::vector<CoordMember *> *' to 'std::vector<Particle *> *' is not allowed
error: no viable overloaded '='
I know for a fact at this point that the data in state->members
are all of type Particle*
. What I don't know is what has to be done to make this cast possible. Any ideas?
tl;dr:
std::vector<Derived*>* ptr = static_cast< std::vector<Base*>* >(&object);
static_cast from 'std::vector<Derived*>*' to 'std::vector<Base*>*' is not allowed
The cast doesn't work because the vector
s are completely unrelated . You'll have to cast each individual object in the vector.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.