Quote:Original post by Sneftel
I'm not saying that it's pragmatically possible in C++. I'm just saying that other than the underlying data representation, there's no good excuse not to be able to extend the type system with that conversion, given that the outward interface of the two types are compatible.
I don't know, you're basically introducing the concept of const semi-inheritance to an already complex system just to be able to convert between two classes which are effectively leaf nodes of a inheritance tree. Aside from type, they also specify concretely the allocator to be used.
A much simpler solution would seem to be to go up the inheritance tree.
While we're unable to inject parent classes into the C++ standard library hierarchy, we are entirely able to do the next best thing and conjure up a secondary reference/view class which can handle this (admittedly nonexistant) parent, and we have ample opportunity to handle upcasting, differing allocation methods, etc. there.
Call it const_vector_ref, to take a page from Boost.MultiArray's naming conventions. Of course, this gets us back to the apparent needless complexity point, and one wonders if the solution couldn't be to just template the function based on the container or iterators.
If that wasn't an option, my next instinct would be to just put together a pair of
industry::virtual_random_access_iterators to
industry::static_cast_iterators, to the original container's iterators, since I've already written this code.
Alternatives to static_cast_iterator would include some as of yet unwritten implicit_cast_iterator, or hacking implicit casting into my virtual iterators.
Since we're already presupposing const access (to avoid the original moral objection raised of DerivedFromBase2* in a DerivedFromBase1* container), that should provide all the necessary access anyways (unless you really really need .capacity(), end-begin for the equivilant of .size() not being enough for your greedy arse). Of course, the virtual access for every single operation may make copying actually superior in performance, but...
(in case you're wondering where the iterator source is)EDIT:
To get back closer to where the OP is probably thinking about, wanting to get connections by index rubs me just about every wrong way possible. Connections come and go, so I'm left wondering why you'd use this presumably unstable key in the first place. Why you have separate collections of XyzConnection that need to be handled elsewhere as collections of Connection also leaves me wondering. Combined with the example identifiers which hopefully don't even exist (XyzConnection), the whole shebang feels horribly over engineered, over thought in the wrong directions, and Java-like. Enterprisey. Hopefully that's just the opaqueness, but it's hard to point out the best direction to go when the original premise feels faulty in the first place. Not enough data.
[Edited by - MaulingMonkey on March 7, 2008 3:52:48 PM]