class template | Sololearn: Learn to code for FREE!


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)?

10/25/2017 2:16:41 PM

Shivani Goyal

3 Answers

New Answer


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.


answer is mother and cout.