Dynamic array creatiion

In example 2 the compiler throws the error that the array must have a constanst size.

However by creating a template Matrix class like in example 1 you can create dynamic Matrixs on the fly. Why is this?

//example 1
1
2
3
4
5
6
7
template< int _size>
class Matrix{
public:
	Matrix(const int *data );
private:
	int elements[_size*_size];
};


//example 2
1
2
3
4
5
6
7
class Matrix{
public:
	Matrix(const int *data, int size ):size(size){};
private:
	int size;
	int elements[size*size];
};
The size of an array must be a value that can be evaluated during compile-time. In other words, the specified size must be constant. MinGW allows non-constants, but that should be avoided for compatibility with other compilers (if you're porting).

Wazzak
Ok so what your saying is that even when I use my template function, you have to declare at compile time what the size is going to be
Yes.

Wazzak
Thanks again
Topic archived. No new replies allowed.