hey, everyone. I am still learning templtes and I have 2 questions:
1-Why is it that all the examples of templates I have seen are about arrays?Is that all templates are used for?

2-after pondering about the above question, I decided to try something else witha template..a generic unction.
However the program just hangs when I run it. Only one object is created(because I always put a sentence in my constructors, to know when an object is created. the sentence appears only once.)
Can someone help me out? Thanks in advance!![

#include<iostream>
#include<string>
using namespace std;

 template<class T>
 class display{
 public:
 T dat1,dat2;
 display():dat1(0),dat2(0){
 cout<<"Display's constructor"<<endl;
 }
 ~display(){
 cout<<"Display's destructor"<<endl;
 }
 T add(T& dat1,T& dat2){
 cout<<dat1 + dat2<<endl<<"add() has executed"<<endl;
 return dat1+dat2;
 }
};
 int main()
{
 int dati1=34,dati2=60;
 string dats1("Tobi is a "),dats2("very good boy");
 display<int>intd;
 display<string>stringd;
 intd.add(dati1,dati2);
 stringd.add(dats1,dats2);
 return 0;
}

"Show me your code and I will tell you who you are.."-Tkud

Edited 7 Years Ago by tkud: n/a

hey, everyone. I am still learning templtes and I have 2 questions:
1-Why is it that all the examples of templates I have seen are about arrays?Is that all templates are used for?

I suppose because containers are the most prominent and easy-to-explain examples when it comes to introducing templates - its probably safe to say that you'll encounter a vector very early on in your C++ learning, and a vector is a great example of an application of templates.
Beyond that, templates can be (*ab)used for some immensely complicated, mind-bending solutions to address some very large and difficult problems.

* = Some would argue that templates were intended to be simple, but the true C++ Guru's in the world discovered that templates were actually capable of almost standing alone as an entirely new turing-complete programming language, albeit one which is incredibly ugly and sometimes hard to understand, but extremely powerful nonetheless.

2-after pondering about the above question, I decided to try something else witha template..a generic unction.
However the program just hangs when I run it. Only one object is created(because I always put a sentence in my constructors, to know when an object is created. the sentence appears only once.)
Can someone help me out? Thanks in advance!!

T dat1,dat2;
 display():dat1(0),dat2(0){

Your initialiser list assumes that dat1 and dat2 are capable of taking 0 as their constructor argument - but for std::string, this will actually do something really dangerous (that zero converts into a NULL pointer, and assumes that you are passing it a character array). In other words, your program is falling over in a big way at this point.

The simplest solution is to require the user of your class to pass in two values to your constructor instead

display(T t1, T t2):dat1(t1),dat2(t2)

The syntax to create your objects will change to this

display<int> intd(0,0);
display<string> stringd("", "");
This question has already been answered. Start a new discussion instead.