简体   繁体   中英

Initialization of vector of objects

Before I put my question let me mention in advance that I am aware that this topic has been discussed a lot, but yet again by looking to most of the answers I failed to figure out the proper way to achieve what I want (most probably because I missed sth from the anwers given). So, say I want to create 20 times a vector of objects (say of size ten each) that should be of global scope (in the sense that this is done inside a class and i want these vectors of objects to be passed outside the class). So, I am really not sure if the following is valid:

for ( int i =0; i<20; i++)
 vector<A> * vec_of_class = new vector<A>(10);
 /*where A is the class and I use new to have them in the heap,
 so that they won't be local */

Now, I know that this means that I should be extra carefull with handling them and make sure that I preper delete them, but I am not sure if the operation I am doing is valid. In general I want to achieve to initialize 20 different vectors of object A that can be used in the global scope (the operation is happening inside a function of an other class), I know that it could be used a vector of vectors of object A, but I would like to find out how to do correctly this kind of operation (initializing 20 different vectors of object A inside the body function of an other class and use them globally). I hope ,my question was not to confusing and you could give me some help here.

There should never be a case when you dynamically allocate any standard container, including vector. So std::vector<> = new <> is always wrong.

To initialize your vector, you can use multitude of techniques. I find initializer lists especially handy, like here:

std::vector<int> vec{10, 20, 30, 40, 50};

If you declare a variable into a function, the scope of that variable will be the function. It won't be accessible from outside.

What you can do is declare the 20 pointers in the global scope, and initialize them wherever you want (inside a function, for instance).

So, say I want to create 20 times a vector of objects (say of size ten each)

That's easy. Use:

std::vector<std::vector<A>> my_data{20, std::vector<A>{10}};

that should be of global scope (in the sense that this is done inside a class and i want these vectors of objects to be passed outside the class).

I don't have a clear picture of what you want to accomplish here.

When something is done inside a class, it could be done in a static member function or a regular member function. It seems like you should use a static member function but I am not sure.

If you use a static member function, I would suggest:

std::vector<std::vector<A>> const& TheClass::get_my_data()
{
   static std::vector<std::vector<A>> my_data{20, std::vector<A>{10}};
   static bool inited = false;
   if ( !inited )
   {
      // Set the values of the elements in my_data
      // ...

      inited = true;
   }
   return my_data;
}

If you use a regular member function, I suggest populating my_data in the constructor and returning from a function.

// Need to make my_data a member variable
TheClass::TheClass() : my_data{20, std::vector<A>{10}}
{
   // Set the values of the elements in my_data
   // ...
}

std::vector<std::vector<A>> const& TheClass::get_my_data() const
{
   return my_data;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM