how to include a templatized variable in another class

A

aurgathor

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
 
S

Sep

aurgathor said:
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

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top