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

前端 未结 5 616
陌清茗
陌清茗 2020-12-19 05:48

I haven\'t worked with c++ in a while, but I just started a project with it. This may not be possible, but Im trying to create a template class with an array that sets its s

相关标签:
5条回答
  • 2020-12-19 06:27

    You have to create the array at run time.

    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, for this to work, T must have a default constructor (that is, a constructor that takes no arguments).

    0 讨论(0)
  • 2020-12-19 06:28

    The reason you're getting compiler errors is this line:

    T this_array[start_size];
    

    This line would make your Tarray actually contain start_size instances of T. It wouldn't hold a pointer or reference to these instances - they would be part of same block of memory that contains the Tarray's other 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 isn't possible.

    There are two ways to solve this:

    1. Allocate the array of T instances on the heap, using array new. This is what std::vector does. Writing such a class and getting it to behave right when it's copied/moved/expanded/etc is difficult and tedious, so I'd recommend just using std::vector instead.
    2. Make the number of T instances fixed, 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'd recommend using one of these instead of writing your own. Unless this is homework, of course...

    Lastly, it's worth noting that this is an error:

    ~Tarray(){
        delete[] this_array;
    }
    

    this_array wasn't allocated with new, so you shouldn't delete it. If the array is part of the class as it is here (rather than being separately heap-allocated and owned by the class), then it will be destroyed along with the rest of the class by default. Calling delete is not only unnecessary, it will almost certainly cause a crash.

    0 讨论(0)
  • 2020-12-19 06:31

    std::vector is precisely 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];
        }
    };
    
    0 讨论(0)
  • 2020-12-19 06:34

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

    (If you want a fixed-size array, then std::array might be a possibility, I think that's in C++11, if not, then boost probably has an implementation).

    If you insist on having that ordinary array syntax, though, as if you were using ye-olde C, then you will need to use a template parameter, such that your template class has two arguments - one for the 'T' it already has now, and another for the array size.

    You are making life especially difficult by managing that array yourself - if you feel you have to define a destructor, you really should define the copy constructor in addition to the constructor. (That's called the Rule Of The Big Three, if I recall correctly), instead, rely on RAII and avoid having to ever explicitly call operator delete or delete[] yourself.

    0 讨论(0)
  • 2020-12-19 06:45

    The following code does something similar but not using the 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 like this:

    TArray<10> myArray;
    
    0 讨论(0)
提交回复
热议问题