Walking through a relatively basic C++ Template
-
02-01-2020 - |
Domanda
Our instructor has asked us to walk through a Template and explain what the various pieces of it are doing:
Template<class T> set<T> set<T>:: setDifference(const Set<T> &that) const
Here is what I've got so far:
Template<class T>: declares the new template
set<T>: states the return type of our template
set<T>: pretty confused about the second set, possibly the class name?
setDifference: Calls on our setDifference function
const Set<T> &that:
Parameters of setDifference, states set<T> cannot be modified within
setDifference or put on the left hand side of the equation. "&that" references "that"
memory location to use/call
const (at the end):
Our function can only be called by a const object of the class
nor can it call any non-const member functions or change member variables.
If anyone could please correct/add to what I already have I would greatly appreciate it.
Soluzione
It's the beginning of the definition of the member function setDifference outside of its class body
template <class T>
struct set{
//...
set setDifference( const set& that) const;
};
template<class T>
set<T> set<T>::setDifference( const set<T>& that) const
{
set<T> newInstance (...);
//implementation of setDifference: newInstance = this - that
return newInstance;
}
- The return type is a newly created instance of the set class
- The second set is indeed the class name; needed because setDifference lives in the namespace
set<T>
- you're right about the rest
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow