Let's say I have a custom implementation of std::vector named my_vector, and they have the same interface.
There are many functions that take std::vector (or its pointer/reference) as input. I want to make as little modification as possible to these functions, while allowing them to accept either std::vector or my_vector.
Replacing these functions with function templates is a way to go, but there seem to be a lot of change (and hence difficult to rollback), and they can't do type checking unless I use std::is_same to further complicate them. Function overloading is another alternative, but it entails duplicating the code, and is also difficult to rollback the change.
Question: can I design a wrapper type Vector for std::vector and my_vector, and let those functions accept Vector (or its pointer/reference) instead? But the problem with polymorphism is std::vector can't inherit a custom class.
Ideally, the code should similar to this (not required to be the same) :
Definition of function:
// void foo(std::vector vec) {...} // old
void foo(Vector vec) {...} // new
Use the function:
std::vector<char> stdvec(5,'a');
my_vector<char> myvec(5,'a');
...
foo(stdvec);
foo(myvec);
Thanks!
Aucun commentaire:
Enregistrer un commentaire