I wrote about this the other day but do not think I explained it too clearly...I shall try again...

So my problem is this...when I am trying to allocate memory to a pointer, it fails.

This is my MatrixClass definition..

class MatrixClass
{
public:
    MatrixClass(int m, int n);
    MatrixClass(void);
    virtual ~MatrixClass(void);
    double getRead(int num1, int num2) const;
    double& getReadWrite(int num3, int num4);
     void set(int i,int j,double value);//set value at i,j to value
    MatrixClass(const MatrixClass &rhs);
    void assign(int M,int N);
    MatrixClass sum1(const MatrixClass& rhs) const;
    MatrixClass operator+(const MatrixClass& rhs);//overloading the + operator 
    inline MatrixClass operator-();
private:

    double* dataptr;
    int M;
    int N;
};

I am trying to do this..

MatrixClass BB;
BB = A + B;

So here is my overloaded + function..

MatrixClass MatrixClass::operator +(const MatrixClass &rhs)
            {
                MatrixClass temp;
                //temp.M = this->M + rhs.M;
                //temp.N = this->N + rhs.N;

                    for(int i = 0;i < M;i++)
                    {
                        for(int j = 0; j<N;j++)
                        {
                            temp.dataptr[i * N + j] = this->getReadWrite(i,j) + rhs.dataptr[i*N+j];
                        }
                    }   
                return temp;
            }//end operator +

Once temp returns...it calls the copy constructor...passing temp as 'rhs' and 'this' would refer to 'BB'? (Am I right in thinking this?)

MatrixClass::MatrixClass(const MatrixClass &rhs)//copy constructor
{
    this->M = rhs.M;
    this->N = rhs.N;
    dataptr = 0;
    if(rhs.dataptr != 0)
    {

        dataptr = new double[M * N];//allocate memory for the new object being assigned to...
        the line here where I try to allocate memory gives me an error.....Am I right in thinking that this would be assigning memory to dataptr of 'BB'?? Values are assigned to 'M' and 'N' fine....


        int num = sizeof(double);
        memcpy(this->dataptr,rhs.dataptr,M*N*sizeof(double));   
    }
    else
    {
        this->dataptr = 0;
    }
}//end copy constructor

Also the error that i get is this... 'Unhandled exception at 0x75a0b727 in assignment.exe: Microsoft C++ exception: std::bad_alloc at memory location 0x002af924..'

Hope someone can help me...I am at a massive loss with this...thanks!

BB = A + B;

  • is overloaded as a member method instead of a friend method.

  • returns a Matrix called temp.

Once temp returns...it calls the copy constructor...passing temp as 'rhs' and 'this' would refer to 'BB'? (Am I right in thinking this?)

To my thinking, in the sequence above, when temp is returned the assignment operator is used to assign temp to BB. The copy constructor wouldn't be used in that sequence, that I can tell.

Thanks for the reply....the copy constructor is used to assign temp to BB...I thought the copy constructor was called to make a copy when something is returned by value?

I just do not understand why 'M' and 'N' will assign fine but 'dataptr' will not

Lerner....could you please explain why the copy constructor is being called to do this job then?

Are you sure it is?

Put an output statement in the copy constructor and the assignment operator that says "copy constructor called" and "assignment operator called" as the first line of the method body. Then do the following.

Matrix A, B;
//initialize A and B with valid data

Matrix BB(A + B); //clearly copy constructor should be called
Matrix BB = A + B; //looks like it should be assignment, but it's really the copy constructor that's used.

Matrix BB;
BB = A + B; //this should be the assignment operator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.