c++ - Inherit from std::vector<T> and overload operator[] for custom indexing -
I want to be able to index a std :: vector such that when I use data through operator [] If I use, then the index is zero, the lower outstanding and the end of the vector is upper.
This is what I am trying to do. It is not certain how to do it in C ++.
using namespace std; Class Provider {Public: The name of the string; }; Template & lt; Class T & gt; Category VEC: Public Study :: Vector & lt; T & gt; {Private Vace () {} Public VAC (Int Upper, Int Comeout) {OO = Upper Bound; Lb = Lower balance; } Public: T & amp; Operator [] (int); Private: Int ub; Int lb; }; //how to do this? T & amp; VecDQ :: Operator [] (Int IDX) {Return (UB-LB) + IDX; } Int main () {int upper bound = 175642; Int lower bound = 175000; // I have a VAC deck and provider & gt; Index such as IDX [0] less outstanding VAC & lt; Std :: deque & lt; Provider & gt; & Gt; VecOfDeq (upper bound, lower bound); // here, VAC & lt; Std :: deque & lt; Provider & gt; & Gt; Fill in here with some random examples // print [175000] .at (1) .name & lt; & Lt; Std :: endl; // VAC [175000] Really VAC [0] Return 0;
There are some typo in your sample code
// How to do it? T & amp; VecDQ :: Operator [] (Int IDX) {Return (UB-LB) + IDX; }
Here you are telling the compiler that you are defining the member function of the operator []
to the VecDQ
class. You have not declared a VecDQ
class, I'm assuming that you mean Vec
except for the class key, that should be within the definition class, because you are a template class , The compiler will not know what "t" is outside of the template class.
This is a positive definition:
T & amp; Operator [] (int idx) {Return to this-> (idx - lb); }
at Vector is a member function of the class that gives a reference to the item in the index. When you have a VC derivative class, whether it needs to subtract lower bound from the given index.
You have to decide whether to change your base vector dynamically (whether a new index is given) or built.
The above change is a VEC creator that has its own program along with the base vector with previously-assigned default-built elements. I also supply a manufacturer for the provider class either to be able to construct it with literal string string or std :: wires.
Comments
Post a Comment