Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Adaickalavan
Created December 27, 2020 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adaickalavan/3bb8646af32e64fb255b85d09c1d3373 to your computer and use it in GitHub Desktop.
Save Adaickalavan/3bb8646af32e64fb255b85d09c1d3373 to your computer and use it in GitHub Desktop.
C++ template operator overload with template class
#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;
}
@Adaickalavan
Copy link
Author

C++ template operator overload for template class

See website for information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment