Click here to Skip to main content
15,879,095 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
While reading some tutorial on template, i stumbled upon a class which was used as a float later while declaring template class object, but couldn't understand why its needed, if any one could explain
C++
class Number {
  float f;
public:
  Number(float ff = 0.0f) : f(ff) {}
  Number& operator=(const Number& n) {
    f = n.f;
    return *this;
  }
  operator float() const { return f; }
  friend ostream&
    operator<<(ostream& os, const Number& x) {
      return os << x.f;
  }
};
Posted
Comments
Jochen Arndt 21-Oct-15 7:01am    
Why it is needed can be only answered when seeing where and how it is used.

The only reason for its existance seems the output operator. When that is not used, there is no reason to use such a class.

[EDIT]
Because you found that in a tutorial, it might be just an example for a template class whithout practical usage.

This snippet comes from here (the last example): http://www.fi.muni.cz/usr/jkucera/tic/tic0169.html[^] and the question demonstrates the problem with taking code out of context.

Eckel is talking about using built in types as template arguments. In my opinion the example is not explained fully.

He could have written:
C++
Holder<float,> h;

and got the same output. However the use of the wrapper class for float, Number allows initialization of the value in the constructor.

Doing this:
C++
int main() {
  Holder<number,> h;
  for(int j = 0; j < 20; j++)
    cout << h[j] << endl;
} ///:~ 


we see that the the values are set to 0 in the constructor of Number.

If we do this:
C++
int main() {
  Holder<float,> h;
  for(int j = 0; j < 20; j++)
    cout << h[j] << endl;
} ///:~ 


then we have uninitialized values.
 
Share this answer
 
v2
Comments
the_beginner 22-Oct-15 7:10am    
So except for initializing values to zero does it serve any other purpose
Andreas Gieriet 22-Oct-15 8:58am    
It seems to serve the purpose to show you how to pass *any* type to a template argument. In the examples before (Array, Stack), there are always built-in types used as template parameters. In the last example, the Holder class gets another class as template parameter. The Number class is just a simple class to show this. Don't take any intrinsic meaning out of the fact that this Number class is not useful for anything.
Do not project too much (meaning) into the example - it only shows various usages of template parameters.
Regards
Andi
The class actually wraps a float in a object. I don't see any reason why it is needed, however (I don't see any code using it as well, however).
 
Share this answer
 
A float is a so called value type. They are just primary values and don't have any instance or even class methods or properties. Value types don't have a class backing them up. When a template<class T> is required, you wouldn't be able to use float or int directly. But when encapsulated in a class like number you can. You now have a reference type instead of just a value type. This Number class is like boxing/unboxing in Java.

Good luck!
 
Share this answer
 
v2
Comments
Andreas Gieriet 21-Oct-15 10:42am    
This is C++, not C# or alike. So, no such thing like value versus reference type. Or am I missing something?
Regards
Andi
E.F. Nijboer 21-Oct-15 16:16pm    
Fundamental type would be a better name in the context of C++.
Andreas Gieriet 21-Oct-15 17:27pm    
Your solution is incorrect also with respect of if float is usable in templates. C++ allows to use any type as template parameter as long as the applied operations (e.g. like comparison or arithmetic, etc.) is supported by that given type.
Regards
Andi
PS: I was tempted to down-vote your solution, but it was not me. I opted for discussion the issue ;-)
E.F. Nijboer 21-Oct-15 18:41pm    
Now that you said I was wrong because templates can use fundamental types I saw that in my answer the "class T" was filtered out. I updated my answer and used html encoding so it is now visible. So what I meant that encapsulating the float in a Number class makes it possible to use them when a template class has this restriction.
Andreas Gieriet 22-Oct-15 3:21am    
Still incorrect information. You seem to mix up C# generics with C++ templates. The class in template<class T> is not restricting the usage of types here - you may use any type, no matter if a class or a struct or an enum or a composed type or a built-in type, etc. Instead of class T you can likewise write typename T without changing the meaning. I wonder if I misread completely your text...? What I read simply does not make sense for C++.
Regards
Andi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900