template and -> symbol and operator
Hi guys.I have a question. why do we use template and -> symbol and operator ???
is this-> necessary below code.?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <iostream>
#include <conio.h>
using namespace std;
template <typename T>
struct obj{
T i;
obj(T j=0) : i(j)
{cout <<'+';}
obj(obj<T>& o2) {this->i=o2.i; cout<<'<';}
~obj() {cout <<'-';};
void swap(obj<T> o)
{
cout <<"s";
T temp =i;
i=o.i;
o.i=temp;
}
T operator/(obj<T>& t) {cout <<'/'; return this ->i/t.i;}
T operator/(int j){cout <<'i'; return this ->i/j;}
};
template <typename T>
void swap (obj<T> o1, obj<T> o2)
{
cout <<"w";
T temp=o1.i;
o1.i=o2.i;
o2.i=temp;
}
int main()
{
obj<int> o1(3.7);
obj<int> o2(5);
obj<int> o3(o1);
swap(o1,o2);
cout<<o1.i<<"_"<<o2.i;
return 0;
}
|
Last edited on
Topic archived. No new replies allowed.