Vector as a class member
Asked Answered
C

4

10

Hello I have this question: I would like to have a vector as class member. This is perhaps my question easier for you and I apologize for that.

  • how should I declare the vector? And is this correct? std::vector<int> *myVector; or std::vector<int> myVector ?
  • how should I handle this vector in dealloc?
  • How can I initialize the array into a if?

Is this correct?

if(myCondition)
{
   if(!myVector) //is this correct?
       myVector = new std::vector<int>(); //is this correct? on this i have a error
}
Cruller answered 18/12, 2011 at 17:36 Comment(0)
W
10

You most certainly want to use std::vector<int> myVector. No need to initialize it, as it gets automatically initialized in the constructor of your class and deallocated when your class is destroyed.

Wicketkeeper answered 18/12, 2011 at 17:41 Comment(0)
S
9

Just use automatic allocation: declare it as a member like this:

class YourClass
{
    std::vector<int> myVector;
    // ...
};

The array gets constructed automatically before any of your constructor is run and is destroyed automatically when your object is deallocated, you don't need to care about it (also, the default copy constructor and assignment operator will handle copying gracefully automatically).

If, instead, you want to create the array only after a particular condition, you have to resort to a (smart) pointer and dynamic allocation, but IMHO it's quite cumbersome (especially because you then have to get right the "big three" - copy constructor, assignment operator, destructor); you could instead simply allocate the vector with automatic allocation and use a separate flag to mark your array as not initialized, or just check if its size is 0.

Sexennial answered 18/12, 2011 at 17:40 Comment(0)
T
1

That depends entirely on context - what the vector means and why you need it. Should it be shared among multiple objects? If you don't know, don't keep a pointer, go with your second option.

std::vector<int> myVector;

If you have strong reasons to have a pointer, then please use a smart pointer, the one that provides most appropriate ownership for your situation - shared_ptr, scoped_ptr, unique_ptr or whatever_ptr

Tomkin answered 18/12, 2011 at 17:41 Comment(0)
M
0

Most of the time, when we use standard library, We do not need to care about the memory allocation/deallocation. The template will handle it automatically. eg. The memory of a std::vector will be increase or decrease according to the elements stored in this vector. This would be an example.

Therefore, almost you can use it this way in your case.

std::vector<int> myVector  //your second declaration
if(myCondition)
{
   myVector.push(some_int);  // use it directly
}

The memory the vector used will be deallocated when the Class object you created is destroyed.

Microfilm answered 18/12, 2011 at 18:18 Comment(1)
The link you provided is broken. Are you referring to this example?Anthia

© 2022 - 2024 — McMap. All rights reserved.