Say I have two classes A
and B
, and a vector of class A
as below:
class A {
int foo;
int bar;
void someMethod();
};
class B {
uint foo;
uint bar;
void someOtherMethod();
};
std::vector<A> va;
and I want to interpret va
as a vector of B, since int
and uint
are re-interpretable.
What is the best practice to do the re-interpretation?
For example, if I want to invoke someOtherMethod()
on va
, I can do ((std::vector<B> *)(&va))->someOtherMethod()
. But is it the best practice?
It seems to me that reinterpret_cast<std::vector<B> >(va).someOtherMethod()
does NOT work.
In addition, I am working on C++03.
-- UPDATE --
Sorry for my misinterpret of my own question. Yet my question will be a lot different than this one. So I created another question here.
I will close this question soon: This question can be seen as an independent question, and I think one of the answers below is good enough to be accepted.
Don't. Any way you think you can do it results in undefined behaviour. Create a new vector and copy over the values.