Skip to main content
GameDev.net gamedev.net
🔒 Locked

Track data access to stl container

Started by aarbron Mar 2, 2016 at 9:06 PM 1 replies 1.3k views
Original Post
aarbron
aarbron

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.

SeanMiddleditch
SeanMiddleditch
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.
Sean Middleditch – Game Systems Engineer – Join my team!
DoctorGlow
DoctorGlow

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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.