.


:




:

































 

 

 

 





++, , (, .. -) . , . , .

#include <iostream>

using namespace std;

#include <string.h>

template <class T1,class T2>

T1 sm(T1 a,T2 b) //

{ return (T1)(a+b); // c

}

template <class T1,class T2,class T3>

T1 sm(T1 a,T2 b,T3 c) //

{ return (T1)(a+b+c); // c

}

int main()

{cout<<" sm(int,int) = "<<sm(4,6)<<endl;

cout<<" sm(int,int,int) = "<<sm(4,6,1)<<endl;

cout<<" sm(int,double) = "<<sm(5,.3)<<endl;

cout<<" sm(double,int,short)= " <<

sm(.4,6,(short)1)<<endl;

// cout<<sm(" "," ++")<<endl; error cannot add two pointers

return 0;

}

:

sm(int,int) = 10

sm(int,int,int) = 11

sm(int,double) = 5

sm(double,int,short)= 7.4

sm(), 2, 3 . . T1,T2 ( ) T1,T2,T3 ( ) . . 1, 2, 3 template <class T1,class T2> template <class T1,class T2,class T3>.

. .

sm() , .. , , . , sm(), sm() . :

char *sm(char *a,char *b) //

{ char *tmp=a; //

a=new char[strlen(a)+strlen(b)+1];

strcpy(a,tmp);

strcat(a,b);

return a;

}

main() , ,

cout<<sm(" "," ++")<<endl;

:

++

, .

template <class T1,class T2>

T1 sm(T1 a,T2 b) //

{ return (T1)(a+b); // c

}

template <class T1,class T2>

T1 sm(T2 a,T1 b) //

{ return (T1)(a+b); // c

}

int main()

{ sm(1.,2) // error 'sm': none of 2 overload have a best conversion

// 'sm': ambiguous call to overloaded function

return 0;

}

. : .

, ( ), .

, . .

 





:


: 2015-09-20; !; : 1011 |


:

:

, .
==> ...

1519 - | 1350 -


© 2015-2024 lektsii.org - -

: 0.01 .