Lets say you have two classes (although in my case one is a struct defined in libc) which share some attributes, but are otherwise different, one of the classes I am able to modify, the other one I am not. In one of the functions I am using I have to pass in a pointer to an array of these structs, yet I would like to see the effects of the call directly in the pairing of the two objects. I have had several ideas as to how to solve this, but they all end up being very unruly and quite unmaintainable. For instance, store the structs in a vector and add a pointer as an attribute to the class I can modify, but that doesn't work as the pointers are generally invalid after erasure (the pointers to the structs after are invalid, but not the ones before the deleted element). Another idea was to use a map between the two, but that doesn't work for a similar reason as I can't use references in the key due to invalidation. Using a duplicate object as a key is also out of the question as that makes deletion quite difficult as well since I need to be able to delete the pairing based on the key or the value.
In my case the objects only have one common attribute, which is unique at a given time, but may not be unique in the lifetime of the process.
I am also restricted to using C++11 as that is the newest version the only compiler I have access to supports.
I'm not really sure how to provide a more helpful code example as it seems sort of trivial, but here is my best attempt:
// Class definitions
class A {
int i;
...
};
class B {
int i;
...
};
void F(A*, int num, ...);
int main() {
...
std::vector<A> as;
std::vector<B> bs;
F(as.data(), as.size(), ...);
...
}
Aucun commentaire:
Enregistrer un commentaire