I wonder if someone could explain the solution to this...
Top class has a vector of Base class. Each derived class of Base class has a different data struct. Base class has a vector of the derived class data struct. Top class needs to access the size of the struct footprint and well as the size of the struct vector and the underlying array.
Top class is blind to the derivatives of the base class.
If I template then Top class needs to be template aware and this will not work. When I derive, top class only see base class. I am not able to make the struct virtual ( this would have been the ideal solution. )
The derived class needs to define, or layout, the struct. Top class needs access to underlying array.
How can I solve this?
#include <iostream>
#include <string>
#include <vector>
class Base {
public:
struct Data {
};
Data data;
std::vector<Data> myData;
};
class Foo1 : public Base {
public:
struct Data {
std::string s1;
std::string s2;
std::string s3;
};
};
class Foo2 : public Base {
public:
struct Data {
int i1;
double d2;
std::string s3;
};
};
class Top {
public:
std::vector<Base> foos;
void test() {
for (std::vector<Base>::iterator f = foos.begin(); f != foos.end(); f++) {
std::cout << "Data Size = " << sizeof((*f).data) << std::endl;
}
}
};
int main(int argc, char* argv[])
{
std::cout << "Test Begin" << std::endl;
Foo1 f1;
Foo2 f2;
Top t;
t.foos.push_back((Base)f1);
t.foos.push_back((Base)f2);
t.test();
std::cout << "Test End" << std::endl;
}