Re: how to include a templatized variable in another class
aurgathor wrote:[color=blue]
> Howdy,
>
> I got the templatized class Matrix working in the way
> it's written in the faq [16.18] , and it works fine as:
> Matrix<sqr_T> display(80,25);
>
> However, I'd like to have this variable in the private
> section of another class, and I'd like to instantiate in
> the class's constructor, with the option of having a size
> determined at run-time.
>
> Currently, my class looks like this:
>
> class UI {
> public:
> UI();
> ~UI();
> //etc
> private:
> sqr_T display[80][25];
> // etc
> }
>
> I know I need to add an UI(short, short), but that's
> the easy part -- I couldn't figure out the proper
> syntax to include the templatized version of display.
>
> Any idea?
>
> TIA[/color]
To add a templatized variable into a class is the same as creating an
automatic variable in a function.
private:
Matrix<sqr_T> display;
Now, to initialize it with the values 80 and 25, just use the
constructor initialization list and pass the variables just as if you
were passing them on declaration.
UI() :
display(80, 25)
{
.....
That is, unless you wanted the size to be determined by the
constructor, in which case your presupposition was correct, add a
constructor for UI that takes two doubles (or whatever the types are
for Matrix's constructor) and pass those to it in the initialization
list. |