1

Is there any way to dynamically (I think that's the right term) define an int array in a class, the size of which is a private member variable in that same class? For example:

class Scene()
{
    //public member functions

    private:

        int max;
        int xcoords[max];
 }

I've searched through other answered questions on here, but I haven't learned how to use vectors in class yet, which is what most responses suggest. Ideally, in my constructor for the class, I'd be passed an int max, with which I then initialize the xcoord array to have a size of max with entries all -1.

2
  • You will have to change the type of xcoords to int* and in the constructor assign memory dynamically as xcoords = new int[max]; Commented Feb 9, 2016 at 0:32
  • 2
    "but I haven't learned how to use vectors in class yet" - Then it's a great time to do that. I very rarely use old C arrays in modern C++ code, and so should you. Commented Feb 9, 2016 at 0:37

2 Answers 2

4

If you want dynamic sizing for your member data, you almost certainly have to store it in the heap. Which means, something like this

class Foo{
  int* data;
public:
  Foo(int size){
    data = new int[size];
  }
  ~Foo(){
    // remember to clean up
    delete[] data;
  }
}

This way, your constructor will allocate size for size ints in the heap memory when the class is created, and free it up when it is deleted.


The more official c++ way of writing the constructor is:

  Foo(int size):
    data(new int[size])
  {
  }

But in the end it will do the same thing.

Sign up to request clarification or add additional context in comments.

7 Comments

My god this has helped me so much. Thank you so much. I've been trying so hard to be persistent in asking questions in my class, but it doesn't seem like people are willing to understand that I am very new to all this. As a result, they keep parroting answers to me which would make sense to other students in my class, or they tell me I should know how do this already. I've been despairing over this one question since it prevents me from progressing, but you've made my week so much more bearable! What I've said may seem overly dramatic, but I mean it nonetheless, thank you.
No problem, if you can use five minutes to save someone an hour, why not :)
In my destructor, do I need to do: delete [] data; data = NULL
This class violates the Rule of Three
delete data or delete[] data?
|
1

Consider leaving memory management to standard libraries. Instead of Arrays, use vectors.

#include <vector>
#include <iostream>

using std::vector;
using std::cout;
using std::endl;

class Scene
{
public:
    vector<int> verticeData;
};

int LEGACY_FUNCTION(const int * data, int count)
{
    for (int i = 0; i < count; ++i)
    {
        cout << " ";
        cout << data[i];
        cout << " ";

    }
    cout << endl;
    return 0;
}

int main()
{
    Scene myscene;
    myscene.verticeData.emplace_back(3);
    myscene.verticeData.emplace_back(4);
    myscene.verticeData.emplace_back(5);

    LEGACY_FUNCTION(myscene.verticeData.data(), myscene.verticeData.size());

    myscene.verticeData.clear();
    myscene.verticeData.emplace_back(1);
    myscene.verticeData.emplace_back(7);

    LEGACY_FUNCTION(myscene.verticeData.data(), myscene.verticeData.size());

    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.