简体   繁体   中英

Defining an array in a class but the size is determined in the constructor

How can I define an array as a member of class while its size determined somewhere else and actually it is passed to the constructor.

More detail: There is an array of integer numbers and is defined a public member in class.

class foo {
  public:
    int *arr[];
    int s;
};

However the size of array is passed in the constructor.

foo::foo()
        : s (array_size)
{
}

What is the correct syntax of doing such thing?

The correct way of doing this is to use the STL and std::vector< int > for such tasks. The following is a rough outline that may work for you:

#include <vector>

...

class foo
{
    public:
        std::vector< int > arr_;

        ...

        foo(const int* numbers, int numberCount)
        : arr_(numbers, numbers+numberCount)
        {
        }

        ...

        int size() const
        {
            return arr_.size();
        }

        ...

        int& operator [] (int index)
        {
           return arr_.at(index);
        }
};

Further information on vectors can be found here . Further suggestions:

  • don't make your instance variables public if there is no compelling reason to do so.
  • name instance variables somehow special (for example by appending a _ ).
  • many people dislike classes that are named all lowercase.

Your class appears to be attempting to define an array of 'pointers to int ', not an array of int as you suggest. However, the classic answer is precisely that you use a 'pointer to int ' and allocate the array in the constructor and release it in the destructor. To a first approximation:

class foo
{
public:
    int *arr;
    int  s;
    foo(int sz) : s(sz) { arr = new int [s]; }
   ~foo()               { delete [] arr; }
};

If you're going to go down this route, you will also need to provide an assignment operator and a copy constructor (as Mike Seymour reminds me - thanks, Mike); the default versions the compiler will write for you if you don't write them for yourself will be wrong - horribly wrong. (The SO question "What is the Rule of Three?" covers this.)

However, this is (probably) not exception safe, and you'd be well advised to use std::vector<int> instead of a plain pointer:

class foo
{
public:
    std::vector<int> arr;
    foo(int sz) : arr(sz) { }
};

You don't need to store the size explicitly; the vector does that for you.

Well first off, not sure why you're using an array of int pointers, but who am I to question your design.. Anywho, you can do this with a pointer and dynamic allocation. Declare your array as...

int **arr;

and initialize it in the ctor as..

*arr = new int*[s];

Remember to clean it up on the dtor with delete[] . Another alternative is to use std::vector<int*>

Maybe this is not exactly what you want, but I would do it with a template parameter

template<int T>
class Foo
{
    public:
    int array[T];
    int s;
};

You instantiate this class like

Foo<1024> * f = new Foo<1024> ();
class foo {
   public:
     std::vector m_vec;
     foo(uint32_t size) : m_vec(size) { }
};

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