0

I am trying to store some objects in an array. Here is a code example for introducing my problem.

It would be nice if you could give me Solution and what could be the consequences?

class Worker
  {
    //Vars
    private int m_id;
    private int m_money;
    //And so on....

  }

class Factory
  {
    //Should I use a double pointer
    private Worker **m_workerarray;

    //A normal pointer 
    private Worker *m_workerarray;

    //Or a worker array
    private Worker[] m_workerarray;

    //I think it´s better for every solution to 
    //store the Array lenght
    private int m_arraylenght;
  }
laurisvr
  • 2,724
  • 6
  • 25
  • 44
RefMa77
  • 283
  • 2
  • 14
  • 1
    http://www.cplusplus.com/reference/stl/ – Othman Benchekroun May 27 '15 at 07:25
  • 1
    If you're still using C-style arrays and raw pointers, why use C++ at all? `` and `` are the two most immediate benefits of C++. I also encourage you to pick up [shared_ptr](http://en.cppreference.com/w/cpp/memory/shared_ptr) early on. – DevSolar May 27 '15 at 07:32
  • Please have a look at [this](http://stackoverflow.com/help/how-to-ask) page. The question in essence is a valid one, but does require a bit of attending to. – laurisvr May 27 '15 at 07:46

4 Answers4

4

You should avoid raw pointers and C-like array. You can use std::vector or std::array

Elried
  • 81
  • 7
2

Just like Eliried mentioned std::vector is probably one of the most common ways that it would be done. Also needed would be #include <vector>

Resulting in something like this: std::vector<Worker> factory;

For more information on vectors and containers you can look here: http://www.cplusplus.com/reference/stl/

This can be done with raw pointers but vectors would be a simpler way to do it

1

I have listed your options and my own in general order of usability. Note that all the options have their uses. Some are just more rare.

using an STL container

This would in almost all cases be the wisest thing to do. Examples of STL containers are std::vector and std::list. If you're like me you might feel that using C style arrays will gain you better performance. This is not necessarily the case. For dynamic size arrays and in some cases even fixed size arrays. The STL containers have equal or better performance to C style arrays.

If you want to know which STL container to use. You might want to have a look at this page.

private Worker[] m_workerarray;

This is the only one I would ever recommend using next to the STL containers. Note that this will only work for a fixed size array. If you have an array with a fixed size, or you intent to make a circular buffer. This is might be the array you would want.

private Worker *m_workerarray;

A normal pointer in general should only be used for objects that might be null and are created dynamically. And even then there are safer ways to do this. I would strongly discourage using them as arrays. Unless you want a very high performance, dynamic array, tailor made to the occasion. An in STD container in general has equal or better performance for most usages.

private Worker **m_workerarray;

This is useful for a so called two dimensional matrix. However, even when using this, in a lot of cases it's not recommended. Since a two dimensional matrix requires several memory jumps. In general I would avoid double pointers unless you really know what you're doing.

Note:

//I think it´s better for every solution to //store the Array lenght private Worker *m_workerarray;

It's not just better, it's imperative for every solution you handed. The only exception being the fixed size array ([]).

Community
  • 1
  • 1
laurisvr
  • 2,724
  • 6
  • 25
  • 44
1

It sort of depends. If your class is going to hold a fixed number, or a known maximum number, of Worker objects then a Worker[] would be appropriate. If you have to worry about resizing the array, the situation is more complex. This is where std::vector comes into its own.

rds504
  • 146
  • 6