Untitled

 avatar
unknown
plain_text
3 months ago
877 B
5
Indexable
#include <iostream>
using namespace std;
class Array
{
private:
    int* data;
    int size;
public:
    Array(int s)
    {
        size = s;
        data = new int[size];
        cout << "Constructor called\n";
    }

    // Move Constructor
    Array(Array&& obj)
    {
        cout << "Move Constructor called\n";

        size = obj.size;
        data = obj.data;

        obj.data = nullptr;   // release ownership
        obj.size = 0;
    }

    void display()
    {
        cout << "data " << data << endl;
        cout << "Size: " << size << endl;
    }

    // Destructor
    ~Array()
    {
        cout << "destructor " << size << endl;
        delete[] data;
    }
};

int main()
{
    Array a1(5);

    // Move constructor called
    a1.display();
    Array a2 = move(a1);
    a1.display();
    a2.display();
}

Editor is loading...
Leave a Comment