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)?
4 Réponses
+ 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.
+ 1
temlate,class,?
0
answer is mother and cout.
0
temlate,class