Work of the overloaded operator during initialization of a Class



  • How do you reload, for example, the assignee operator to work during initialization of the class?

    #include <iostream>`
    using namespace std;`
    

    class Matrix
    {
    private:
    int **Arr;
    int Size;
    public:
    Matrix(int SizeOfMatrix);
    Matrix operator = (Matrix &);
    };

    int main()
    {
    Matrix A(4);
    Matrix B(4) = A;
    system("pause");
    return 0;
    }
    Matrix::Matrix(int SizeOfMatrix)
    {
    Size = SizeOfMatrix;
    Arr = new int *[Size];
    for (int i = 0; i < Size; i++)
    {
    Arr[i] = new int[Size];
    for (int j = 0; j < Size; j++)
    Arr[i][j] = rand() % 50;
    }
    }
    Matrix Matrix :: operator =(Matrix &A)
    {
    for (int i = 0; i < Size; i++)
    {
    for (int j = 0; j < Size; j++)
    Arr[i][j] = A.Arr[i][j];
    }
    return *this;
    }



  • At the outset, I should like to point out that this proposal is a proposal.

    Matrix B(4) = A;
    

    incorrect and not compiled. In fact, the proposal contains two initials: 4 and A

    But even if you write correctly.

    Matrix A(4);
    Matrix B = A;
    

    This is not the place where you think the accusator is called, but the copying design, which is defined by the compiler incorrectly, and which is simply replicating the members of the objects.

    When you dynamically distribute memory in your class, i.e. use indexes, you have to identify clearly at least a copy design, a copying operator and a designer. Otherwise, the behaviour of your program may be uncertain. For example, the use of a copy designer created by the compiler may result in two objects being indexed to the same area of memory, and therefore an attempt will be made to remove the same memory twice at the disposal of these facilities.

    Your class may look as follows.

    class Matrix
    {
    private:
        int **Arr;
        size_t Size;
    public:
        explicit Matrix( size_t );
        Matrix( const Matrix & );
        ~Matrix();
        Matrix & operator = ( const Matrix & );
    };
    

    Matrix::Matrix( size_t SizeOfMatrix ) : Arr( nullptr ), Size ( SizeOfMatrix )
    {
    if ( Size )
    {
    Arr = new int *[Size];

        for ( size_t i = 0; i &lt; Size; i++ )
        {
            Arr[i] = new int[Size];
            for ( size_t j = 0; j &lt; Size; j++ ) Arr[i][j] = rand() % 50;
        }
    }
    

    }

    Matrix::Matrix( const Matrix &rhs ) : Arr( nullptr ), Size ( rhs.Size )
    {
    if ( Size )
    {
    Arr = new int *[Size];

        for ( size_t i = 0; i &lt; Size; i++ )
        {
            Arr[i] = new int[Size];
            for ( size_t j = 0; j &lt; Size; j++ ) Arr[i][j] = rhs.Arr[i][j];
        }
    }
    

    }

    Matrix::~Matrix()
    {
    for ( size_t i = 0; i < Size; i++ ) delete [] Arr[i];
    delete [] Arr;
    }

    Matrix & Matrix::operator =( const Matrix &rhs )
    {
    if ( this != &rhs )
    {
    int **tmp = nullptr;

        if ( rhs.Size != 0 )
        {
            if ( Size != rhs.Size )
            {
                tmp = new int *[rhs.Size];
    
                for ( size_t i = 0; i &lt; rhs.Size; i++ )
                {
                    tmp[i] = new int[rhs.Size];
                }
            }
            else
            {
                tmp = Arr;
            }
    
            for ( size_t i = 0; i &lt; rhs.Size; i++ )
            {
                for ( size_t j = 0; j &lt; rhs.Size; j++ ) tmp[i][j] = rhs.Arr[i][j];
            }
        }
    
        if ( Size != rhs.Size )
        {
            for ( size_t i = 0; i &lt; Size; i++ ) delete [] Arr[i];
            delete [] Arr;
        }
    
        Arr = tmp;
        Size = rhs.Size;
    }
    
    return *this;
    

    }




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2