Determine array size in constructor initializer

前端 未结 12 2357
失恋的感觉
失恋的感觉 2020-12-08 21:10

In the code below I would like array to be defined as an array of size x when the Class constructor is called. How can I do that?

class Class
{
public:
  int         


        
相关标签:
12条回答
  • 2020-12-08 21:46

    I had the same problem and I solved it this way

    class example
    {
      int *array;
    
      example (int size)
      {
        array = new int[size];
      }
    }
    
    0 讨论(0)
  • 2020-12-08 21:47

    Sorry for necroing this old thread. There is actually a way to find out the size of the array compile-time. It goes something like this:

    #include <cstdlib>
    
    template<typename T>
        class Class
        {
            T* _Buffer;
    
            public:
            template<size_t SIZE>
                Class(T (&static_array)[SIZE])
                {
                    _Buffer = (T*)malloc(sizeof(T) * SIZE);
    
                    memcpy(_Buffer, static_array, sizeof(T) * SIZE);
                }
    
                ~Class()
                {
                    if(_Buffer)
                    {
                        free(_Buffer);
                        _Buffer = NULL;
                    }
                }
        };
    
    int main()
    {
        int int_array[32];
        Class<int> c = Class<int>(int_array);
    
        return 0;
    }
    

    Alternatively, if you hate to malloc / new, then you can create a size templated class instead. Though, I wouldn't really recommend it and the syntax is quite ugly.

    #include <cstdio>
    
    template<typename T, size_t SIZE>
        class Class
        {
            private:
                T _Array[sz];
            public:
                Class(T (&static_array)[SIZE])
                {
                    memcpy(_Array, static_array, sizeof(T) * SIZE);
                }
        };
    
    int main()
    {
        char int_array[32];
        Class<char, sizeof(int_array)> c = Class<char, sizeof(int_array)>(int_array);
        return 0;
    }
    

    Anyways, I hope this was helpful :)

    0 讨论(0)
  • 2020-12-08 21:48

    Use the new operator:

    class Class
    {
       int* array;
       Class(int x) : array(new int[x]) {};
    };
    
    0 讨论(0)
  • 2020-12-08 21:48

    Two options:

    Use std::vector. This allows easy re-sizing of the array.
    Use std::tr1::array. This has a static size.

    Both can be correctly initialized in the constructors initializer list.

    0 讨论(0)
  • 2020-12-08 21:49

    You can't initialize the size of an array with a non-const dimension that can't be calculated at compile time (at least not in current C++ standard, AFAIK).

    I recommend using std::vector<int> instead of array. It provides array like syntax for most of the operations.

    0 讨论(0)
  • 2020-12-08 21:52

    Like already suggested, vector is a good choice for most cases.

    Alternatively, if dynamic memory allocation is to be avoided and the maximum size is known at compile time, a custom allocator can be used together with std::vector or a library like the embedded template library can be used.

    See here: https://www.etlcpp.com/home.html

    Example class:

    
    #include <etl/vector.h>
    
    class TestDummyClass {
    public:
        TestDummyClass(size_t vectorSize) {
            if(vectorSize < MAX_SIZE) {
                testVector.resize(vectorSize);
            }
        }
    
    private:
        static constexpr uint8_t MAX_SIZE = 20;
        etl::vector<int, MAX_SIZE> testVector;
        uint8_t dummyMember = 0;
    };
    
    0 讨论(0)
提交回复
热议问题