• Advertisement
Sign in to follow this  

Track data access to stl container

This topic is 785 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

Hello,

I got these vectors where I want to monitor the access, and do something on every write.

What is the best approach? I planned on using protected inheritance of std vector just because there's less boilerplate than wrapping it.

Share this post


Link to post
Share on other sites
Advertisement
With protected inheritance, you still have to wrap everything, since otherwise nobody can access the vector member that you're trying not to wrap.

With public inheritance, you end up with the slicing problem: someone can just implicitly cast your type to std::vector<...>& and bypass your code without even realizing that they're doing it.

With C++ as it stands today, wrapping is pretty much your only real choice. C++17 may offer respite via operator. (aka smart references, aka proxy objects) but that isn't available even in preview form yet as it's still undergoing design and debate in the committee.

Share this post


Link to post
Share on other sites

You could, but keep in mind that none of the methods on vector are virtual, including dtor, so beware if you and up passing your derived class to functions that will operate on std::vector. There is enough discussions on the web that you can search for to get more info.

Share this post


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

  • Advertisement