I have a 3rd-party class, say, class A
, and a function accepting vector of class A
from the same 3rd-party, say f3()
(See simplified program below).
For easier use of A
, I created a derived class B
. Many part of my program used class B
.
The question is, how can I call f3()
with a vector of B
as its argument?
Is a forced casting in the argument of f3()
like the program below a good practice?
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// a 3rd-party class
class A
{
public:
int n;
void f1();
};
// my class
class B: public A
{
public:
void f2();
};
// a 3rd-party function
void f3(std::vector<A> &a);
int main()
{
std::vector<B> y;
y.push_back(B());
y.push_back(B());
y.push_back(B());
y.push_back(B());
f3(*(vector<A>*)(&y)); // Is this a good practice?
cout << y[3].n << endl;
return 0;
}
Note that, for compatibility, I purposely make class B
to have no more variables than class A
, when B
inherits from A
. However, Class B
does have more methods than A
.
Will it guarantee that sizeof(A)
is the same as sizeof(B)
, so that our cast of vector will work?
I am working on C++03