Template question
Is there any way I can change the value of a template parameter inside the templated object or are these values constant. i.e.
template<int width, height>
class matrix {
............
}
void matrix<width,height>::setSize(int w,int h) {
width = w;
height = h;
}
Talguy
Junior Poster in Training
96 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
when you declare the class you can do this
template<typename T, typename T>
class matrix
{
//...
// or
template <typename T, typename C>
class matrix
{
//...
the first example both numbers passed will be the same type that you chose the second example the first and second term can be different ie. etc.
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
Not really the answer to what I was asking. so my template is:
template matrix {..........}
I declare the object like this
matrix <3,3> foo
The numbers defined in the template will determine the size of the 2d array inside the object. so the matrix I declared is a 3x3 matrix. If I want to make foo into a 4x4 I would like to call foo.resize(4,4) where the method would set the template parameters from <3,3> to <4,4>.
Is this possible. Or do I have to delete the object and redeclare the with the new parameters I would like.
Talguy
Junior Poster in Training
96 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
why are using a template for this application? you could just pass the values into the constructor in the matrix class and have a resize function built in if you need to change it. templates are used to make functions or classes that can have or use different types and are used instead of having to write a function or class for each different type.
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
Template parameter are instantiated pre-runtime. So when your program is running, you cannot change them.
If you are making a Matrix class, why don't you consider vector or vectors as the backend data structure. Then make your own resize function. Alternatively, you can create a copy constructor. Then, when you wish to resize the object, create a bigger new object, copy to it the content of original object and destroy the former.
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
why are using a template for this application? you could just pass the values into the constructor in the matrix class and have a resize function built in if you need to change it. templates are used to make functions or classes that can have or use different types and are used instead of having to write a function or class for each different type.
Yea I was just thinking of that. I would defiantly be easier doing it that way. I was doing it the template way so that I could make some predefined tyedef'd matrices that the user could call quickly and load their data into.
Talguy
Junior Poster in Training
96 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
no prob happy to help if you have any other errors or logic faults let me know nd ill happy to help.
NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
>>why are using a template for this application?
Perhaps to save the run-time
siddhant3s
Practically a Posting Shark
816 posts since Oct 2007
Reputation Points: 1,486
Solved Threads: 140
Talguy
Junior Poster in Training
96 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0