class template | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

class template

template <class T> class Pair { private: T first, second; public: Pair (T a, T b): first(a), second(b){ } T bigger(); }; template <class T> T Pair<T>::bigger() { // some code } pls tell me that what is the role of first(a),second(b)?

25th Oct 2017, 2:16 PM
Shivani Goyal
4 Answers
+ 4
This is called member initialization. When all you need to do is assign the data read in a constructor specifically to its appropriate member (one having the same type), you can, instead of writing : MyClass(T x, T y) { first = x; second = y; } Can do this, thanks to member initialization: MyClass(T x, T y) : first(x), second(y) { } The syntax is just simple : the name of the member used as a function with the argument as the value to be assigned, and if there are multiple such assignments, seperate each with a comma.
25th Oct 2017, 2:51 PM
Kinshuk Vasisht
Kinshuk Vasisht - avatar
+ 1
temlate,class,?
27th Nov 2021, 5:20 AM
Abdurahimov Hurrambek
0
answer is mother and cout.
14th Jan 2020, 1:17 PM
Imanuel Wicaksono
Imanuel Wicaksono - avatar
0
temlate,class
27th Apr 2022, 7:27 PM
Hossam Mohammed Elbesh
Hossam Mohammed Elbesh - avatar