Last active
August 29, 2015 14:02
-
-
Save SiegeLord/85ced65ab220a3fdc1fc to your computer and use it in GitHub Desktop.
C++ matrix moving
This file contains hidden or 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
clone | |
move | |
move | |
100 200 300 |
This file contains hidden or 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 <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