Results 1 to 3 of 3
Thread: templates
Hybrid View
-
3rd May 2014 12:30 #1Registered User
Join Date: Aug:2012
Location:
Posts: 297
templates
, templates C++. : -
?Code:template <class T> typedef T (*function_template)(T, T)
,
, :Code:function_template<double> f
Code:double f(double, double)
Code:template <class T>
?Code:template <typename T>
Code:void fon(int a) { cout << a << endl; }
. , , . , - 3225906 . , - , .Code:fon('192');
. :
, ,Code:int a; cout << a;
0. , default int, ?Code:int a = int(); cout << a;
, class Car { ....}
default ?Code:Car my_car;
.
-
4th May 2014 19:16 #2Registered User
Join Date: Aug:2003
Location:
Posts: 175
, template typedef . C++11 using:
foo<> T- . bar<T> . C++98, typedef- . :Code:template <class T> using foo = T(T,T); template <class T> using bar = T(*)(T,T); double mix(foo<double> f, bar<int> g) { return f(2.5,3.75)+g(4,5); } double ddd(double a, double b) { return a*b; } int nnn(int a, int b) { return a^b; } void main() {std::cout<<mix(ddd,nnn); }
baz<T>::t bag<T>::t. "typename baz<T>::t".Code:template <class > struct baz { typedef T t(T,T); }; template <class > struct bag { typedef T (*t)(T,T); };
:
. , . (typename) , , , class .Code:template <class T> ... template <typename T> ...
. . Visual Studio- int - , . , : 3225906 0x313932, ASCII '1', '9' '2'. , ( , ).Code:fon('192');
. : , . int() , 0/0.0/false. -: . template-:
x , . "T x;", default- , . .Code:template <class T> void f() { T x = T(); }
( MSVS ) . C++11 . , .
. Visual C++ GCC.
-
7th May 2014 09:50 #3Registered User
Join Date: Aug:2012
Location:
Posts: 297
, . , new :
double- 0. , ( () ) default ? 2 . - ? default- .Code:double* pointer = new double[100]();
, , :
(.., ). , - "Error 1 error C2678: binary '*' : no operator found which takes a left-hand operand of type 'const Polynomial' (or there is no acceptable conversion)" .Code:Polynomial Polynomial::operator*(double number) { Polynomial result = *this; for (size_t i = 0; i <= degree; ++i) { result.coefficients[i] *= number; } return result; } Polynomial operator*(double number, /* const */ Polynomial& other) // ? { return other*number; }
const( -) . , const , . , . . 1-2 const, , .
.




Reply With Quote
Lenovo ThinkPad 15 IdeaPad 15
5th May 2023, 22:16 in