Connecting Tech Pros Worldwide Help | Site Map

how to include a templatized variable in another class

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 01:59 AM
aurgathor
Guest
 
Posts: n/a
Default how to include a templatized variable in another class

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




  #2  
Old July 23rd, 2005, 01:59 AM
Sep
Guest
 
Posts: n/a
Default 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.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.