简体   繁体   中英

Can someone help me explain what the second line of the code means? This is in C++

My professor gave us this piece of code to study to better understand how class templates work in C++ but I'm confused on what the second line means. I put two asterisks (**) next to the statement I'd like more clarification on.

template<typename T>
Pair<T>::Pair(const T& firstVal, const T& secondVal) //**
{
     first = firstVal;
     second = secondVal;
}

This is the Pair class template:

template<typename T>
class Pair
{
public:
     Pair();
     Pair(const T& firstVal, const T& secondVal);
   
     void setFirst(const T& newVal);
     void setSecond(const T& newVal);

     T getFirst() const;
     T getSecond() const;

     ~Pair(){} 

private:
     T first, second;
};

 

What I'm confused about is why she's treating the constructor as a function and defining it as the class has no member functions with the name "Pair" apart from the constructor? I'm sure I'm misunderstanding something about constructors and functions and if someone could help I'd greatly appreciate it, thank you!

The mentioned snippet is an out-of-class definition for a constructor of a class template. In particular, to define a member function(which a constructor also is) of a class template, we have to specify that it is part of a template and so we have to use the full type qualification of the class template.

For instance, say you want to have an out-of-class definition for the member function setFirst , then you would define it as follows:

template<typename T> //parameter clause goes here
void Pair<T>::setFirst(const T& newVal)  //full type qualification goes here
{
    
}

Similarly, if we want to have an out-of-class definition for the constructor then we will do it as follows:

template<typename T>//parameter clause is here
Pair<T>::Pair(const T& firstVal, const T& secondVal)//full type qualification is here
             : first(firstVal), second(secondVal)
{
     
}

Note that a constructor is a special member function.

Well, Pair() and Pair(const T& firstVal, const T& secondVal) are both functions. Constructors are functions that are called when the object is initialised, and you can define and then implement the constructors later, just like any other normal member function.

A non-templated class's constructor implementation would look like this:

myClass::myClass(/* my args ... */) {
   // do stuff ...
}

And, if myClass is a templated class, you would have to have type T defined somewhere, and that would be done like so:

template <typename T>
myClass<T>::myClass(/* my args ... */) {
    // do stuff ...
}

This lets you also define specific specialisations, say if you want your class's constructor to have specific functionality for when T = int . You would define it using the template!

template<>
myClass<int>::myClass(/* my args ... */) {
    // do special integer stuff!
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM