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

48,810

Solution 1

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.

Solution 2

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];
    }
};

Solution 3

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;

Solution 4

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).

Share:
48,810
sinθ
Author by

sinθ

Updated on July 09, 2022

Comments

  • sinθ
    sinθ almost 2 years

    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 size to the value of a constant which i'm trying to set with the constructor.

    This is the code of the constructor:

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

    This is the code that sets the array size:

    const int start_size;
    T this_array[start_size];
    

    This is the entire 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 have been changing as I try to tweak the code, but these are the errors from this particular build.

    Thanks for any help

  • Arafangion
    Arafangion about 12 years
    This code will be very fragile - what happens if you accidentally copy it?
  • sinθ
    sinθ about 12 years
    Thanks, but I still get errors like "'Tarray<T>::start_size' cannot appear in a constant-expression"
  • Arafangion
    Arafangion about 12 years
    @Mike G: If you're using std::vector, then there is no point in keeping a ::start_size. That said, you've specified 'const' there without giving it a value. (And you can't do THAT in a class like that)
  • iammilind
    iammilind about 12 years
    this_array.reserve(s) would have been better.
  • David Rodríguez - dribeas
    David Rodríguez - dribeas about 12 years
    @iammilind disagree, the semantics would be completely different as the elements would not be created if only reserve is called.
  • kamprath
    kamprath about 12 years
    The question was how to create vectors in a template, which I answered. However, no accidental copying should occur because a constructor has been declared (which prevents the compiler from auto creating constructors), but no copy constructor has been declared. As a result, the compiler will complain if there were any implicit copies occurring. Fragility averted. I'd appreciate the -1 taken away, thank you.
  • iammilind
    iammilind about 12 years
    @DavidRodríguez-dribeas, I know, it's just reserved. And that is the purpose. With vector::reserve() one can initialize the elements on need bases. If out of size s, only half is going to be used then no point in creating whole array.
  • Arafangion
    Arafangion about 12 years
    The compiler will auto-generate a copy-constructor for you. If you want to specify no copy-constructor, you need to provide it as an explicitly private constructor, but then fail to provide an implementation. Also, in order for me to remove the -1, the answer needs to be updated.
  • kamprath
    kamprath about 12 years
    My bad, I was confused with default constructors. Updated.