Thread: Why is destructor being called??

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    64

    Why is destructor being called??

    Hello all,

    I have created a dynamic matrix class. I overloaded the multiplication operator as follows:

    Code:
    Matrix Matrix::operator*(const Matrix &another) const //'const' keyword after => doesn't
    {														//modify data members
    	if (col != another.row)
    	{
    		cout<<"Error in matrix multiplication. Matrix dimensions do not agree."<<endl;
    		return *this;
    	}
    
    	Matrix prod(row,another.col);
    	Complex sum(0,0);
    
    	for(int i=0; i<row; i++)		//i: 0 to 3
    	{
    		sum = 0;
    		for(int j=0; j<another.col; j++)	//j: 0 to 2
    		{
    			for(int k=0; k<row; k++)
    			{
    				sum = sum + A[i][k] * another(k,j);
    			}
    			//cout<<"prod("<<i<<","<<j<<") = "<<sum<<endl;
    			prod(i,j) = sum;
    		}
    	}
    	return prod;
    }
    I wanted to do it in such a way that if I do

    Code:
    Matrix A(5,5), B(5,1);
    //define the matrices
    cout<<A*B<<endl;   //I overloaded the cout operator as well
    Just for the record, I created a complex number class, and I have defined a similar function:

    Code:
    Complex Complex::operator +(const Complex &num) const
    {
    	Complex sum;
    	sum.real = real + num.real;
    	sum.imag = imag + num.imag;
    	return sum;				
    }
    When I do

    Code:
    Complex a(1,1), b(2,-2);
    cout<<a+b<<endl;
    The destructor is called after it is displayed. But this does not work for the Matrix class, and I just can't figure out why. I thought I would save you all the trouble of going through all the code by only displaying what is pertinent. If you need all the code, pls let me know. Thanks

    Bill

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Bill u was suppose to post this on C++ form not in here. showing a bit more code of your would help us

    ssharish2005

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    Oh I'm sorry. I must have came here by mistake. Thanks.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    C++ is a 'by value' language. The variable 'prod' is a local variable. Thus, the local copy is destroyed when the function ends, and it returns a copy of it. Thus, the destructor of 'prod' is called; it also creates a new copy, and returns it. Or at least I believe that's what's happening. I don't use C++ really ever.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    But why is it working with the Complex function and not with the matrix function?

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You're saying that on this line:
    Code:
    sum = sum + A[k] * another(k,j); // In your overloaded operator*
    the destructor is never called on the variable in your operator+? Nor is it called on the two temp variables in your operator*?
    Last edited by SlyMaelstrom; 03-19-2006 at 10:12 PM.
    Sent from my iPadŽ

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Destructor being called on SGI hash_map key
    By cunnus88 in forum C++ Programming
    Replies: 4
    Last Post: 02-11-2009, 12:05 AM
  2. Replies: 4
    Last Post: 09-21-2008, 02:27 PM
  3. callback from exe called with system()
    By leonv in forum C Programming
    Replies: 3
    Last Post: 01-25-2008, 04:12 PM
  4. AAARG!!! mental block, what is this character called: ')'
    By compjinx in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-29-2002, 10:29 PM
  5. Program ive been working on called ChatMate
    By dirkduck in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-23-2002, 09:05 PM