C++ Constructors and 2D Arrays

Hey guys, I have to create a class to store a string + an int value assigned to the string. Ex : Cat = 6. It must have a constructor which initializes a dynamic 2d array (default size being 2x5) and it must also be able to resize itself when it runs out of space. I'm not really sure how to resize the array once its been initialized. Any help would be awesome! :D

The code for my constructor so far:

1
2
3
4
5
6
7
8
9
 class MyClass {
public:
   typedef int* ArrayPtr;
   ArrayPtr *p1 = new ArrayPtr[2];
   for(int i = 0; i < 2; i ++) {
       p1[i] = new int[5];
     }
}

Also, could someone point me in the general direction on how to make a deconstructor for my d-array?

Thanks a lot!
That is confusing. On one part you need to do a class that stores a string and an int, but on the other part you need to do a class that stores a 2D array of unknown type. Please, explain a bit more.
Sure, I'll do my best.

So basically there's a 2x5 array.

1 2 3 4 5
6 7 8 8 10

each of the array values points to another array which store the string and int values. I'm just lost as to how I would go about storing the items into the array within the constructor. I know how to do it with basic functions but within a class >> constructor, I'm really lost as to how I would go about doing that.

Is this for homework? Do you have certain restrictions? Probably the best way to make an array that can be resized is using std::vector. But if you want to map strings to integers, an even better way would be std::map<std::string, int>.

Usually, whenever you call new[] you have to call delete[] for it too. Right now, you're saving the pointers in temporary variables that will disappear when they go out of scope. This is a memory leak.

Another thing: I wouldn't advise using a typedef for a simple type like int*. Using a pointer to a C++ object is something you do regularly, you don't want to create a typedef for each type. In this case it's even more confusing because you're using an int**, but since it's written like ArrayPtr* it <looks> like it's only a pointer.

To you last question: The <destructor> (not deconstructor) needs deletes like your constructor had news, so once you have p1 as a class member you can use:

1
2
3
for (int i = 0; i < 2; ++i)
  delete[] p1[i];
delete[] p1;

@BasV Yeah this is for an assignment, we can't use vectors.

As for your suggestion, I've tried to delete it the way you've suggested but it keeps giving me an error.

1
2
3
4
for(int i = 0; i < 2; i ++) {
     delete[] p1[i];
   }
delete[] p1;


when i get to the end it keeps telling me p1 is not defined.... This is one of the things throwing me off as well
Yes, this is because p1 is a temporary variable. When you have a constuctor like this:

1
2
3
Myclass() {
  int** p1 = new int*[2];
}


the variable p1 will be out of scope outside of this constructor. Variables are valid from their declaration to the end of their scope (which is often the next matched } bracket).
To make sure the variable stays in existence, you have to make it part of your class object:

1
2
3
4
5
6
7
class MyClass {
  public:
  MyClass(); // Constructor
  ~MyClass(); // Destructor
  private:
  int** int_2d_array_; // Your class variable
}


Hope that helps you! Good luck!
@BasV Hey thanks so much!

Just one more quick question, how would you set the default values of the 2d array to output a value of "[<empty>, 0]" when it is created? I'm thinking of using a loop and just loop it around to equal "[<empty>, 0]" but is there an easier way?

Thanks!
each of the array values points to another array which store the string and int values.
how would you set the default values of the 2d array to output a value of "[<empty>, 0]"

If the values point to another array, then they should point to an element in that other array that has the desired value.

However, I would rather mimic std::vector. The std::vector has two properties: size and capacity.
The capacity tells for how many elements the vector has allocated memory.
The size tells how many of those are actually in use.

It would be logical that your class to report size as 0 at start, even though it has allocated memory for 10 numbers.
Topic archived. No new replies allowed.