Determine array size in constructor initializer

前端 未结 12 2356
失恋的感觉
失恋的感觉 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:31

    I don't think it can be done. At least not the way you want. You can't create a statically sized array (array[]) when the size comes from dynamic information (x).

    You'll need to either store a pointer-to-int, and the size, and overload the copy constructor, assignment operator, and destructor to handle it, or use std::vector.

    class Class
    {
      ::std::vector<int> array;
      Class(int x) : array(x) { }
    };
    
    0 讨论(0)
  • 2020-12-08 21:32

    You folks have so overcomplicated this. Of course you can do this in C++. It is fine for him to use a normal array for efficiency. A vector only makes sense if he doesn't know the final size of the array ahead of time, i.e., it needs to grow over time.

    If you can know the array size one level higher in the chain, a templated class is the easiest, because there's no dynamic allocation and no chance of memory leaks:

    template < int ARRAY_LEN > // you can even set to a default value here of C++'11
    
    class MyClass
      {   
      int array[ARRAY_LEN]; // Don't need to alloc or dealloc in structure!  Works like you imagine!   
      }
    
    // Then you set the length of each object where you declare the object, e.g.
    
    MyClass<1024> instance; // But only works for constant values, i.e. known to compiler
    

    If you can't know the length at the place you declare the object, or if you want to reuse the same object with different lengths, or you must accept an unknown length, then you need to allocate it in your constructor and free it in your destructor... (and in theory always check to make sure it worked...)

    class MyClass
      {
      int *array;
    
      MyClass(int len) { array = calloc(sizeof(int), len); assert(array); }   
      ~MyClass() { free(array); array = NULL; } // DON'T FORGET TO FREE UP SPACE!
      }
    
    0 讨论(0)
  • 2020-12-08 21:35

    Don't you understand there is not need to use vector, if one wants to use arrays it's a matter of efficiency, e.g. less space, no copy time (in such case if handled properly there is not even need to delete the array within a destructor), etc. wichever reasons one has.

    the correct answer is: (quoted)

    class Class
    {
       int* array;
       Class(int x) : array(new int[x]) {};
    };
    

    Do not try to force one to use non optimal alternatives or you'll be confusing unexperienced programmers

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

    You can't do it in C++ - use a std::vector instead:

    #include <vector>
    
    struct A {
       std::vector <int> vec; 
       A( int size ) : vec( size ) {
       }
    };
    
    0 讨论(0)
  • 2020-12-08 21:41

    Declare your array as a pointer. You can initialize it in the initializer list later through through new.

    Better to use vector for unknown size.

    You might want to look at this question as well on variable length arrays.

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

    Instead of using a raw array, why not use a vector instead.

    class SomeType {
      vector<int> v;
      SomeType(size_t x): v(x) {}
    };
    

    Using a vector will give you automatic leak protection in the face of an exception and many other benefits over a raw array.

    0 讨论(0)
提交回复
热议问题