This is a classic "C with classes" approach to C++. In reality, this isn't what any seasoned C++ programmer would write. For one, using raw C arrays is pretty much universally discouraged, unless you're implementing a container library.
Something like this would be more appropriate:
// Don't forget to compile with -std=c++17
#include <iostream>
using std::cout; // This style is less common, but I prefer it
using std::endl; // I'm sure it'll incite some strongly opinionated comments
#include <array>
using std::array;
#include <algorithm>
#include <vector>
using std::vector;
class MyClass {
public: // Begin lazy for brevity; don't do this
std::array<int, 5> arr1 = {1, 2, 3, 4, 5};
std::array<int, 5> arr2 = {10, 10, 10, 10, 10};
};
void elementwise_add_assign(
std::array<int, 5>& lhs,
const std::array<int, 5>& rhs
) {
std::transform(
lhs.begin(), lhs.end(), rhs.begin(),
lhs.begin(),
[](auto& l, const auto& r) -> int {
return l + r;
});
}
int main() {
MyClass foo{};
elementwise_add_assign(foo.arr1, foo.arr2);
for(auto const& value: foo.arr1) {
cout << value << endl; // arr1 values have been added to
}
for(auto const& value: foo.arr2) {
cout << value << endl; // arr2 values remain unaltered
}
}
addmethod that operates on its internals directly? Just, why?addmethod that takes arguments but doesn't exist as part of a class. Just a pure function for adding two arrays together.