Created
December 27, 2020 21:38
-
-
Save Adaickalavan/3bb8646af32e64fb255b85d09c1d3373 to your computer and use it in GitHub Desktop.
C++ template operator overload with template class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using std::ostream; | |
using std::vector; | |
using std::cout; | |
template<class T> | |
class List { | |
private: | |
std::vector<T> vec; | |
public: | |
void push_back(T t){ | |
vec.push_back(t); | |
}; | |
template<class U> | |
friend ostream& operator<<(ostream& os, const List<U>& L ); | |
}; | |
template<class T> | |
ostream& operator<<(ostream& os, const List<T>& L ){ | |
for (T it : L.vec){ | |
os << it << " .. "; | |
} | |
return os; | |
} | |
int main(void){ | |
List<int> L; | |
L.push_back(1); | |
L.push_back(2); | |
L.push_back(3); | |
std::cout << L; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
C++ template operator overload for template class
See website for information.