≈ the.bay.news

Learning how templates and operator() work!

programming.dev
Learning how templates and operator() work!
#include <iostream> template <typename T, const int length=5> class vector { public: T v[length]; vector operator +(const vector &obj) { vector tmp; for(int i = 0; i < length; i++) tmp.v[i] = v[i] + obj.v[i]; return tmp; /* This method below only works if length is a fixed size return {v[0] + obj.v[0], v[1] + obj.v[1], v[2] + obj.v[2], v[3] + obj.v[3], v[4] + obj.v[4]};*/ } }; int main() { vector<int> a={1,2,3,4,5}; vector<int> b={1,2,3,4,5}; auto c = a + b; std::cout << "(int) "; for(int i = 0; i < 5; i++) std::cout << c.v[i] << " "; std::cout << std::endl; }

#include <iostream> template <typename T, const int length=5> class vector { public: T v[length]; vector operator +(const vector &obj) { vector tmp; for(int i = 0; i < length;…

0 comments

Sign in to join the discussion — your thebay.events account works here.

No comments yet.