That should actually work.
A working example
#include <iostream>
#include <vector>
using namespace std;
namespace sparky
{
namespace maths
{
class vec2
{
vector <int> v;
vec2 & add( const vec2 & other)
{
for (size_t n=0; n<v.size() && n<other.v.size(); ++n)
{
v[n] += other.v[n];
}
return *this;
}
public:
vec2 & operator += ( const vec2 & other );
void push_back(const int i){ v.push_back(i); }
friend ostream & operator << ( ostream & os, const vec2 & v2);
};
}
}
namespace sparky
{
namespace maths
{
ostream & operator << (ostream & os, const vec2 & v2)
{
for (const auto & x : v2.v)
os << x << " ";
return os;
}
vec2& vec2::operator+=(const vec2& other)
{
return add(other);
}
}
}
int main()
{
sparky::maths::vec2 v,w;
v.push_back(1);
v.push_back(2);
v.push_back(3);
w.push_back(10);
w.push_back(20);
cout << "v " << v << ", w " << w << "\n";
v += w;
cout << "v " << v << ", w " << w << "\n";
}