I have a custom container template class that stores some data as
private:
T _Data;
to retrieve one, one would call the Data() function
T & Data()
{
return _Data;
}
Now I want to create a specialized case where my data is a std::map<key, data>. However, to retrieve the contents I would now have to call:
MyContainer.Data()[key] = 12;
Which is very clumsy. I'd like to be able to call [] directly on the container, as such
MyContainer[key] = 12;
Naturally, I can do this through template specialization... but then, I must copy the whole template class, with ALL it's functions. This isn't very convinient because it adds extra unnecessary code and, if in the future I decided to change some implementation of my container, I'd need to go in and change the specialization as well. Furthermore, it isn't even necessary as I am not changing any of the template functions, but merely adding a new function (operator[]).
Thus, is there any way to add this extra functionality WITHOUT the overhead of having to copy and re-define all of my base template class parameters?
I cannot define a global operator[] (like I could with operator=) so that's out of the question, and I do not want to create a base and derived classes as, the way my container works, it would also result in copy/pasting lots of code, with an added dynamic memory management on top of it for polymorphism...
(Also, as a side note, about templates - I know I can put full template function declarations in my headers as they will not be compiled like regular functions, thus I won't get any "function foo already defined." I am assuming I can do the same with template classes even if not all of my class's functions are template functions - thus I can define the whole template class (even its non template methods) in the header, correct?)