• Advertisement
Sign in to follow this  

[C++] Boost::Serialize on class derived from std::vector

This topic is 3002 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using the BEAGLE puppy library for some genetic programming stuff, and I need to code in the ability to serialize a tree so that I can resume the evolution process after quitting the program. The problem is, I don't know if I can (or how to) get boost::serialize to work with something like this: class Tree : public std::vector<Node> { public: template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & mFitness; ar & mValid; } float mFitness; //!< Fitness value of the GP tree bool mValid; //!< Flag indicating whether the fitness is valid. }; They key being that the class is derived from std::vector<Node>. It's also a very difficult problem to google because of the wording - "boost::serialize from std::vector derived class" was the best wording I could think of, and it didn't get me any useful results. Some help on this would be hugely appreciated :)

Share this post


Link to post
Share on other sites
Advertisement
Why in the world are you deriving from std::vector in the first place? The SC++L containers aren't meant for deriving.

Share this post


Link to post
Share on other sites
I didn't write BEAGLE Puppy =/

If I knew how without having to change a billion [] and size() accesses throughout BEAGLE puppy, I would change the fact that Tree is derived from std::vector<Node>, but unfortunately I can't...

is there a workaround?

Share this post


Link to post
Share on other sites
Quote:
Original post by Ultima2876
I didn't write BEAGLE Puppy =/

If I knew how without having to change a billion [] and size() accesses throughout BEAGLE puppy, I would change the fact that Tree is derived from std::vector<Node>, but unfortunately I can't...

I'm pretty sure MaulingMonkey's suggestion is the right one. Alternatively, however, you could change Tree to have a vector as a private member, and simply write operator[] and size() and whatever to delegate to it. That's only really a reasonable proposition if the code is using the base class interface in a very limited way.... if it's taking a Tree::const_reverse_iterator and whatnot, it's probably not worth the pain to expose the full interface.

Share this post


Link to post
Share on other sites
Write your own operator[] and size() in Tree, then change Tree to contain a std::vector<Node> as one of its private members, and have the new operator[] and size() call vector_[k] and vector_.size().

Share this post


Link to post
Share on other sites
MaulingMonkey's suggestion is correct - this worked perfectly. Thanks!

Sneftel, you're right, it's also using other std::vector methods (including iterators) dotted around the place.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement