How would I create an Array class that can have lower bounds other than zero (in C++)?

Peter Griffin

This is what I have so far. It throws an exception when I try to access an index out of bounds. I tried to say "return NULL" if the range is out of bounds for the overloaded subscript operator, but it's not working. The problem is when I try to assign a value to an index above the upper limit it allows it to happen. Like with the current code if I change the "< 8" in the main function to "< 9", it uses array element 8 without problem, but I want it to have a problem with that. Any help is appreciated.

#include <iostream>
#include <stdexcept>
using namespace std;

//L for lower-bound, U for upper-bound
template <typename T, int L, int U>
class LBArray
{
    public:
      LBArray()
      {
        lbound = L;
        ubound = U;
        data = new T[ubound - lbound];
      }
      T& operator[](int index)
      {
        if (index < lbound || index > ubound)
        {
          throw out_of_range("index out of bounds");
        }
        return data[index - lbound];
      }
      ~LBArray()
      {
        if (data) delete[] data;
      }
    private:
      T *data;
      int lbound;
      int ubound;
};

int main(int argc, char** argv)
{
  LBArray<int, 5, 7> data;
  cout << "LBArray<int, 5, 7> data\n";
  for (int x = 5; x < 8; x++)
  {
    data[x] = x;
    cout << endl << "data[" << x << "] = " << data[x];
  }
  return 0;
}

Sleeper32

You create array from 5 to 7, and I suppose that 5 and 7 included, then you have 3 elements data[5], data[6], data[7], but in your code:

data = new T[ubound - lbound];

and that 2 elements 7-5 = 2. You lose one element. Therefore I think you need do like that:

data = new T[ubound - lbound + 1];

After that change all work fine, but you do not use try..catch, then your code shutdown. If you do not want to use try..catch, I offer to you next code:

T& operator[](int index)
{
    if (index < lbound || index > ubound)
    {
        T nullVar = NULL;

        return (T&)nullVar;
    }
    return data[index - lbound];
}

Attempting to get element with wrong index the function return NULL.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I import my data from Parse? I would like to import data from a user class other than the class

From Dev

How can I import my data from Parse? I would like to import data from a user class other than the class

From Dev

How can I make bandwidth intensive applications have lower priority than other apps like web browsing/skype

From Dev

How can I put a bit mask on /dev/zero so that I can get bytes other than zero?

From Dev

JSON result with "0" name (zero name) - how can I create correct class in C#?

From Dev

Can/Should I create a owl/rdfs class that would have only 1 instance

From Dev

In iOS 9, if using a class other than UIViewController for a page, how can I create a constraint to avoid clobbering the status and/or tab bars?

From Dev

Why can user have lower permissions than group or other? Who can change them?

From Dev

With directive comparing two inputs to make sure one is lower than the other, how can I trigger calculations on change of either input?

From Dev

How would I import tables into another class (/object??) so I can run queries on it in the other class/object? [slick 3.0][scala]

From Dev

How would I import tables into another class (/object??) so I can run queries on it in the other class/object? [slick 3.0][scala]

From Dev

How can List have size less than zero in java?

From Dev

How I can detect when other control changes it bounds?

From Dev

How would you get the update method to be called in a class other than MyScene in Objective-C's Sprite-Kit project?

From Dev

Why is the size of my class zero? How can I ensure that different objects have different address?

From Dev

How can i change all the values of a multidimensional array which are greater than zero to 1 in python?

From Dev

How would I create a class with a variable name in the class name?

From Dev

How can i create pivot_table with pandas, where displayed other fields than i use for index

From Dev

How can i have at sign in property name in c# Class

From Dev

How can I create instances of a ruby class from a hash array?

From Dev

How can I make my Form Control variables acessible to classes other than my Form class?

From Java

How can I create a Git repository with the default branch name other than "master"?

From Dev

How can I create an event in a time zone other than the calendar's time zone?

From Dev

How can I create a catch that ensures nothing other than a number is entered into a textbox but also accepts a blank entry

From Dev

Can a class use an __init__ method that would create other instances of the same class?

From Dev

Why enumeration objects can have values other than their enumerators in C?

From Dev

Can I have null attribute and other attribute at the same tag in XML created by XSD C# generated class?

From Dev

I have a number and I would like to get the closest number less than and closest number greater than inside a jquery array

From Dev

How can I create a pair of immutable Scala instances that have references to each other?

Related Related

  1. 1

    How can I import my data from Parse? I would like to import data from a user class other than the class

  2. 2

    How can I import my data from Parse? I would like to import data from a user class other than the class

  3. 3

    How can I make bandwidth intensive applications have lower priority than other apps like web browsing/skype

  4. 4

    How can I put a bit mask on /dev/zero so that I can get bytes other than zero?

  5. 5

    JSON result with "0" name (zero name) - how can I create correct class in C#?

  6. 6

    Can/Should I create a owl/rdfs class that would have only 1 instance

  7. 7

    In iOS 9, if using a class other than UIViewController for a page, how can I create a constraint to avoid clobbering the status and/or tab bars?

  8. 8

    Why can user have lower permissions than group or other? Who can change them?

  9. 9

    With directive comparing two inputs to make sure one is lower than the other, how can I trigger calculations on change of either input?

  10. 10

    How would I import tables into another class (/object??) so I can run queries on it in the other class/object? [slick 3.0][scala]

  11. 11

    How would I import tables into another class (/object??) so I can run queries on it in the other class/object? [slick 3.0][scala]

  12. 12

    How can List have size less than zero in java?

  13. 13

    How I can detect when other control changes it bounds?

  14. 14

    How would you get the update method to be called in a class other than MyScene in Objective-C's Sprite-Kit project?

  15. 15

    Why is the size of my class zero? How can I ensure that different objects have different address?

  16. 16

    How can i change all the values of a multidimensional array which are greater than zero to 1 in python?

  17. 17

    How would I create a class with a variable name in the class name?

  18. 18

    How can i create pivot_table with pandas, where displayed other fields than i use for index

  19. 19

    How can i have at sign in property name in c# Class

  20. 20

    How can I create instances of a ruby class from a hash array?

  21. 21

    How can I make my Form Control variables acessible to classes other than my Form class?

  22. 22

    How can I create a Git repository with the default branch name other than "master"?

  23. 23

    How can I create an event in a time zone other than the calendar's time zone?

  24. 24

    How can I create a catch that ensures nothing other than a number is entered into a textbox but also accepts a blank entry

  25. 25

    Can a class use an __init__ method that would create other instances of the same class?

  26. 26

    Why enumeration objects can have values other than their enumerators in C?

  27. 27

    Can I have null attribute and other attribute at the same tag in XML created by XSD C# generated class?

  28. 28

    I have a number and I would like to get the closest number less than and closest number greater than inside a jquery array

  29. 29

    How can I create a pair of immutable Scala instances that have references to each other?

HotTag

Archive