Declaring an array inside a class and setting its size using the constructor

I did not work with C ++ after a while, but I just started a project with it. It may not be possible, but I'm trying to create a template class with an array that sets its size to a constant value that I am trying to set using the constructor.

This is the constructor code:

Tarray(int s): start_size(s){ } 

This is the code that sets the size of the array:

 const int start_size; T this_array[start_size]; 

This is the whole file:

 #ifndef TARRAY_H_ #define TARRAY_H_ template<typename T> class Tarray { private: const int start_size; T this_array[start_size]; int array_size; public: Tarray(int s): start_size(s){ } ~Tarray(){ delete[] this_array; } T & operator[](int i){ return this_array[i]; } }; #endif /* TARRAY_H_ */ 

These are the errors I get:

 ..\/template_array/Tarray.h:16:24: error: 'Tarray<T>::start_size' cannot appear in a constant-expression ..\/template_array/Tarray.h:16:34: error: 'new' cannot appear in a constant-expression ..\/template_array/Tarray.h:16:34: error: ISO C++ forbids initialization of member 'this_array' [-fpermissive] ..\/template_array/Tarray.h:16:34: error: making 'this_array' static [-fpermissive] ..\/template_array/Tarray.h: In instantiation of 'Tarray<Person>': ..\Human.cpp:17:24: instantiated from here ..\/template_array/Tarray.h:16:34: error: invalid in-class initialization of static data member of non-integral type 'Person*' Build error occurred, build is stopped Time consumed: 343 ms. 

The error messages change when I try to configure the code, but these are errors from this particular assembly.

Thanks for any help

+6
source share
5 answers

The reason you get compiler errors is the line:

 T this_array[start_size]; 

On this line, your Tarray really contains start_size instances of T It will not contain a pointer or a link to these instances - they will be part of the same memory block that contains the Tarray instance variables. This would make the class size depend on start_size, and start_size is not known at compile time. The size of any C ++ class must be known at compile time, this is not possible.

There are two ways to solve this problem:

  • Select an array of T instances on the heap using the new array. This is what std::vector does. Writing such a class and its proper behavior when copying / moving / extending / etc are difficult and tedious, so I would recommend using std::vector instead.
  • Commit the number of instances of T and pass it as a template parameter

i.e:.

 template<typename T, std::size_t N> class TArray { ... T this_array[N]; ... } 

This is what std :: array (C ++ 11 only) and boost :: array do. Again, I would recommend using one of them instead of writing your own. If it's not homework, of course ...

Finally, it is worth noting that this is a mistake:

 ~Tarray(){ delete[] this_array; } 

this_array not allocated new , so you should not delete it. If the array is part of the class as it is (instead of having to separate the heap separately and belong to the class), it will be destroyed by default along with the rest of the class. The delete call is not only superfluous, it will almost certainly cause a failure.

+9
source

std::vector is exactly the tool for this job:

 template<typename T> class Tarray { private: std::vector<T> this_array; public: Tarray(int s): this_array(s){ } ~Tarray(){ } T & operator[](int i){ return this_array[i]; } }; 
+2
source

The following code does something similar, but does not use a constructor:

 #ifndef TARRAY_H_ #define TARRAY_H_ template<int SizeT> class Tarray { private: T this_array[SizeT]; public: Tarray() {} ~Tarray() {} T & operator[](int i){ return this_array[i]; } }; #endif /* TARRAY_H_ */ 

and you can use it as follows:

 TArray<10> myArray; 
+2
source

You need to create an array at runtime.

 template<typename T> class Tarray { private: const int start_size; T* this_array; int array_size; Tarray( const Tarrat& inObj ); // no copy public: Tarray(int s): start_size(s), this_array( new T[s] ) { } ~Tarray(){ delete[] this_array; } T & operator[](int i){ return this_array[i]; } }; 

Note that for this, T must have a default constructor (that is, a constructor that takes no arguments).

+1
source

Use std :: vector instead and make life simple for yourself. :)

(If you need a fixed-size array, then std :: array may be an option, I think in C ++ 11, if not, boost probably has an implementation).

If you insist that this is normal array syntax, although as if you were using ye-olde C, you will need to use the template parameter, so your template class has two arguments: one for "T", it already exists, and the other for the size of the array.

You make life especially complicated by managing this array yourself - if you feel that you need to define a destructor, you really have to define a copy constructor in addition to the constructor. (This is called the Rule of the Big Three, if I remember correctly) instead rely on RAII and avoid ever explicitly invoking the delete or delete [] operator yourself.

0
source

Source: https://habr.com/ru/post/909967/


All Articles