John Goche schrieb:
Hello,
I was wondering what the pros and cons are for using
numbers in templates rather than in constructors as in:
MyBuffer<10foo;
rather than
MyBuffer foo(10);
Think of MyBuffer as some kind of array/vector.
With MyBuffer<10you would have an array of fixed size. The size has to be
known at compile-time and has to be a compile-time constant, as with plain
arrays. Each template instantiation forms a new _type_ (the size is part of
the type), and this types are not interchangeable (you cannot pass a
MyBuffer<10where a MyBuffer<3is expected). This way is usefull, for
example, for vectors in maths, where it doesn't make sense to add a
3D-vector to a 4D-vector.
The size as constructor parameter can be a dynamic value, determined at
run-time. As in std::vector, the size can be changed later and is not part
of the type.
--
Thomas
http://www.netmeister.org/news/learn2quote.html