4 Replies - 1067 Views - Last Post: 19 December 2001 - 07:07 PM Rate Topic: -----

#1 RexChaos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-December 01

Why template?

Posted 19 December 2001 - 06:47 AM

Hello all,
 I have developed a container to hold some special kind of object (eg
Complex, Real and Integer objects, they are all derived from a object
named Number). See the example

class MyContainer
{
 .
 .
 .
 public:
   // constructors
   MyContainer(Number obj) { ... };
}

After that, I have seen the book <<Thinking in C++>>. In that book,
the author strongly recommend using the template. The code mentioned
above become

template <class T>;
class MyContainer
{
 .
 .
 .
 public:
   // constructors
   MyContainer(T obj) { ... };
}

The author says that the second approach is better than the first one. I don't
know why?
If I choose to use the template, how can I verify the type of the
class, I only want some class derived from Number exist in the
container. How can I do the identification of class in runtime?
Thanks in advance.


Is This A Good Question/Topic? 0
  • +

Replies To: Why template?

#2 nighthawk  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 1,269
  • Joined: 11-April 01

Re: Why template?

Posted 19 December 2001 - 01:24 PM

i learned about templates in my cs class this semester, and i really have yet to figure out what their importance is, i thought that it made my programming more difficult...
Was This Post Helpful? 0
  • +
  • -

#3 malkiri  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 364
  • Joined: 29-March 01

Re: Why template?

Posted 19 December 2001 - 02:56 PM

Templates are handy when you want to have one general data structure that can handle different data types depending on the context. Most (all?) of the STL containers are templated, so that if you want a vector of characters, you just do:
vector<char> someVector;


Whereas if you wanted a vector of strings, you'd do:
vector<string> someOtherVector;


I'm not sure it would be all that useful in your case, since you're already deriving the three classes from the Number class. As far as using templates, and then checking what the type is at run-time...I've consulted Stroustrup, and I didn't see any way of doing this. There might be one, but I wouldn't be too surprised if there isn't. Templates seem to be most applicable when you don't care about the type, so it would make sense to me that there wouldn't be a way to check it.
Hope this helps.

-Malkiri

Was This Post Helpful? 0
  • +
  • -

#4 RexChaos  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 19-December 01

Re: Why template?

Posted 19 December 2001 - 06:29 PM

yes. I understand the usage of template. As my case, if the code is
template <class T>
class MyContainer
{ T t;
 public:
   void f(void)
   {
     t = abs(t);
   }
};

void main(void)
{MyContainer<Complex> c;
MyContainer<float> f;
...  
}

What do you think? Will it occur error while compile time?
I think so. You see that float is a build-in type, there is no abs() function corresponding to float-type (we should use fabs()). And in Complex class, we have already defined abs function, so it's no problem for Complex template.

Was This Post Helpful? 0
  • +
  • -

#5 nighthawk  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 1,269
  • Joined: 11-April 01

Re: Why template?

Posted 19 December 2001 - 07:07 PM

ah, so that's the use of templates....i understand now...thanks to rex for posing this question :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1