Oct 29, 2009 at 1:37am UTC
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!
Oct 29, 2009 at 1:59am UTC
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.)
Oct 29, 2009 at 2:12am UTC
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.
Oct 29, 2009 at 3:36am UTC
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)
Nov 1, 2009 at 2:47pm UTC
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 Nov 1, 2009 at 2:48pm UTC