Making an array with a “starting index” other than 0 in C++
Running
Source Code
main.cpp
#include "lbarray.h"
int main(int argc, char** argv)
{
LBArray<int, 5, 12> data;
cout << "LBArray<int, 5, 12> data\n";
for (int x = 5; x < 13; x++)
{
data[x] = x;
cout << "\ndata1[" << x << "] = " << data[x];
}
return 0;
}
lbarray.h
#ifndef ARRAY_H
#define ARRAY_H
#include <iostream>
using namespace std;
//L for lower-bound, U for upper-bound
template <typename T, int L, int U>
class LBArray
{
public:
LBArray()
{
data = new T[U - L + 1];
}
T& operator[](int index)
{
return data[index];
}
private:
T* data;
};
#endif