Templates

What does this line of code mean?

Int Holder.first=10;

I am confused, how is a string taking a integer value? Is it a variable taking a vaule?
I am reading the C++ All In One Desk Reference by Jeff Cogswell. This is a line of code in a example for templates.

Also this is my first time ever posting a blog : ). Any help is grateful!
This is a forum ;)

A string is not taking an integer value. int Holder.first = 10; refers to the first variable in the Holder class, which happens here to be type int.

Hope this helps. (Sorry Duoas.)
I'm pretty sure you can't do that at all. When assigning to a class's member you don't have the type there.
Yeah, that code is invalid as you have posted it >_>

Let me guess, they also use void main and #include <conio.h> ? (Don't do either)
Thanks, that helps alot. (QWERTYman).

Oh, here's the full source code:

Listing 5-1 Using Templates to Create Several Versions of a Class


#include <iostream>
#include <stdlib.h)
template <typename T>
class CoolHolder {
public:
T first;
T second;
T third;
T sum() {
return first + second + third;
}
};
int main(int argc, char *argv[])
{
CoolHolder<int> IntHolder;
IntHolder.first = 10;
IntHolder.second = 20;
IntHolder.third = 30;
CoolHolder<int> AnotherIntHolder;
AnotherIntHolder.first = 100;
AnotherIntHolder.second = 200;
AnotherIntHolder.third = 300;
CoolHolder<float> FloatHolder;
FloatHolder.first = 3.1415;
FloatHolder.second = 4.1415;
FloatHolder.third = 5.1415;
cout << IntHolder.first << endl;
cout << AnotherIntHolder.first << endl;
cout << FloatHolder.first << endl;
CoolHolder<int> *hold;
int loop;
for (loop = 0; loop < 10; loop++) {
hold = new CoolHolder<int>;
hold->first = loop * 100;
hold->third = loop *120;
cout << hold->sum() << endl;
delete hold
}
return 0;
}



Also how do you use the formats when composing a fourms. Ex.Source code format(#)






Last edited on
Topic archived. No new replies allowed.