Skip to content

Instantly share code, notes, and snippets.

@SiegeLord
Last active August 29, 2015 14:02
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 SiegeLord/85ced65ab220a3fdc1fc to your computer and use it in GitHub Desktop.
Save SiegeLord/85ced65ab220a3fdc1fc to your computer and use it in GitHub Desktop.
C++ matrix moving
clone
move
move
100 200 300
#include <vector>
#include <iostream>
struct Matrix
{
std::vector<float> data;
Matrix()
{
}
Matrix(const Matrix& m) :
data(m.data)
{
std::cout << "clone" << std::endl;
}
Matrix(Matrix&& m) :
data(std::move(m.data))
{
std::cout << "move" << std::endl;
}
Matrix& operator= (Matrix&& m)
{
std::cout << "move" << std::endl;
data = std::move(m.data);
return *this;
}
};
Matrix operator* (const Matrix& m, float f)
{
Matrix m2(m);
for(auto& v: m2.data)
v *= f;
return m2;
}
Matrix operator* (Matrix&& m, float f)
{
Matrix m2(std::move(m));
for(auto& v: m2.data)
v *= f;
return m2;
}
int main()
{
Matrix m;
m.data = {1.0, 2.0, 3.0};
Matrix m2 = m * 2.0 * 5.0 * 10.0;
for(auto v: m2.data)
{
std::cout << v << " ";
}
std::cout << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment