Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <memory>
- class Vector
- {
- public:
- Vector(int size) : sz(size){
- elem = new double[sz];
- for (int i = 0; i < sz; ++i)
- elem[i] = i;
- }
- Vector(const Vector& other) : sz(other.sz) {
- elem = new double[sz];
- for (int i = 0; i < sz; ++i)
- elem[i] = other[i];
- }
- Vector& operator=(const Vector& a)
- {
- std::cout << "Called Vector's copy assignment operator\n";
- sz = a.sz;
- double* p = new double[sz];
- for (int i = 0; i < sz; ++i)
- p[i] = a[i];
- delete[] elem;
- elem = p;
- return *this;
- }
- Vector(Vector&& other) {
- elem = other.elem;
- sz = other.sz;
- other.elem = nullptr;
- other.sz = 0;
- }
- Vector& operator=(Vector&& other)
- {
- elem = other.elem;
- sz = other.sz;
- other.elem = nullptr;
- other.sz = 0;
- return *this;
- }
- ~Vector() {
- delete[] elem;
- }
- Vector& operator+(const Vector& other)
- {
- for (int i = 0; i < sz; ++i)
- elem[i] += other[i];
- return *this;
- }
- double &operator[](int pos) {
- return elem[pos];
- }
- const double &operator[](int pos) const {
- return elem[pos];
- }
- private:
- int sz;
- double* elem;
- };
- int main(int argc, char* argv[])
- {
- Vector a(100000000);
- Vector b(100000000);
- Vector c(100000000);
- Vector d = std::move(a+b+c);
- std::cout << "d[0] is " << d[0] << ", d[1] is " << d[1] << "\n";
- std::cout << "Press a key to exit...\n";
- std::cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement