Reputation: 1365
I'm making a wrapper for a SQL database and the objects I return are shared_ptr
.
I'd like to support array-type access (ie row["column"]
instead of row->get("column")
). The objects the shared_ptr
stores support array-type access, but of course the shared_ptr
doesn't.
How should I do this with shared_ptrs
, would I need to extend the class?
Upvotes: 1
Views: 286
Reputation: 20730
The most obvious thing is to add an operator[]
to shared_ptr
.
That means define your-own shared_ptr reusing std::shared_ptr and having the [] operator working the way you want.
This can be done in a quick and dirty way by deriving shared_ptr (note: shared_ptr are not polymorphic objects, so don't mix up std:: and yours in a same context, so that you can reuse their interface).
If you are a "don't derive if the destructor isn't virtual" fan, than it is up to you to embed std::shared_ptr in yourptr
, and rewrite the shared_ptr interface in yourptr
one, delegating the functionality you have to retain.
Upvotes: 2