If you have a bunch of structs:
The obvious answer is to make clever use of search and replace to make them inherit from the new base class. It wouldn't be able to access the members of the structs, but you could store pointers to them all in containers
struct base {
int dostuff() const {std::cout << "thing";}
};
struct thing1 : public base {
int size;
};
//otherstructs inheriting from base
int main() {
std::vector<std::unique_ptr<base>> objects;
objects.push_back(std::unique_ptr<base>(new thing1()));
objects[1].dostuff();
}
If making the base class thing is too hard, you might try adding functions via a wrapper class instead. You won't be able to store pointers to these in a container, but they can access members, as long as the structs have similar members.
struct thing1 {
int size;
};
//other unchanged structs
template<class mystruct>
struct wrapper : mystruct {
int getsize() {return mystruct.size;}
};
int main() {
wrapper<thing1> mything1;
std::cout << mything1.size << mything1.getsize();
}
Or, if you're just adding methods, make them separate functions:
struct thing1 {
int size;
};
//other unchanged structs
template<class T>
int getsize(const T& obj) {return obj.size;}
int main() {
thing1 mything1;
std::cout << mything1.size << getsize(mything1);
}