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[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 I could do this:

    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 a million.

    Bill

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You didn't have to repost this, a moderator would have moved your other topic to this forum. But as I said in the other topic:

    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*?
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    I am saying that the destructor is called. But for some reason, when I do

    Code:
    Complex a, b;
    cout<<a+b<<endl; //after assigning values to a and b
    It is called after the result is displayed. When the code is as follows:

    Code:
    Matrix A, B;
    cout<<A*B<<endl; //after assigning values to A and B
    the destructor is called before A*B is displayed, so that part is essentially blank.

  4. #4
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    Am I even going about this the right way? If you do

    Code:
    int a = 9;
    cout<<a+5<<endl;
    cout<<a<<endl;
    then the output should be

    Code:
    14
    9
    i.e. the value of a is not supposed to change after you do a+5. That is why I am creating an extra variable prod and not returning *this. I do not want the contents to be modified unless an assignment operator is used.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes, that's correct.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    So no idea why I can't display A*B (with A and B being matrices)?

  7. #7
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Can I see the prototype for your operator<<.

    Are you passing a reference to the object or the actual object?
    Sent from my iPadŽ

  8. #8
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    Sure. Here is the code:

    Code:
    ostream& operator <<(ostream &out, Matrix &a)
    {
    	for (int i=0; i<a.row; i++)
    	{
    		for (int j=0; j<a.col; j++)
    		{
    			out<<a(i,j)<<"		";
    		}
    		out<<endl;
    	}
    	return out;
    }
    I am passing and returning by reference.

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Matrix prod(row,another.col);
    'prod' is a local variable of the operator*() function. It is destroyed when the function returns. This:
    Code:
    return prod;
    calls the copy constructor for your class. What does your copy constructor look like?
    Last edited by 7stud; 03-19-2006 at 11:31 PM.

  10. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    I understand that it is destroyed. My question is why is it destroyed before it is displayed? Why not after, as is the case with Complex objects?

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Your ostream extraction should take a const reference to an object.
    Sent from my iPadŽ

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    #include <iostream>
    using namespace std;
    
    
    class Test
    {
    public:
    	Test()
    	{
    		a = 10;
    	}
    
    	~Test()
    	{
    		cout<<"Test destructor called."<<endl;
    	}
    
    	friend ostream& operator<<(ostream& out, const Test& obj);
    
    private:
    	int a;
    };
    
    ostream& operator<<(ostream& out, const Test& obj)
    {
    	out<<obj.a;
    	return out;
    }
    
    Test someFunc()
    {
    	Test a;
    	return a;
    }
    
    int main()
    {
    	
    	cout<<someFunc()<<endl;
    
    	return 0;
    }
    output:

    Test destructor called.
    10
    Test destructor called.

  13. #13
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    You mean like this?

    Code:
    ostream& operator <<(ostream &out, Matrix const &a)
    {
    	for (int i=0; i<a.row; i++)
    	{
    		for (int j=0; j<a.col; j++)
    		{
    			if((bool)a == false)
    				a(i,j).SetFreqMode(false);
    			out<<a(i,j)<<"		";
    		}
    		out<<endl;
    	}
    	return out;
    }
    Thanks for the input. I tried it, but it still didn't work.

  14. #14
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    Thanks 7stud. My output however would have the "Destroying ..." message before and after, but no 10.

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I was doing some similar tests:
    Code:
    #include <iostream>
    using namespace std;
    
    class Complex {
       friend ostream& operator<<(ostream&, const Complex&);
       private:
         static int count; 
         int a;
         int b;
         int c;
       public:
         Complex(int i, int j) : a(i), b(j) { c = ++count; }
         ~Complex() { cout << "Destructor called on object " << c << "." << endl; }
         Complex(const Complex& copy) {
              a = copy.a;
              b = copy.b;
              c = ++count;
              cout << "Copy constructor called." << endl;
         }
         Complex operator*(const Complex& operand) {
              Complex three(0,0);
              three.a = a * operand.a;
              three.b = b * operand.b;
              
              return three;
         }
    };
    
    ostream& operator<<(ostream& file, const Complex& obj) {
       return file << "(" << obj.a << "-" << obj.b << ")" << endl;
    }
    
    int Complex::count = 0;
    
    int main() {
        Complex one(2,4);
        Complex two(8,5);
        cout << one * two;
        
        cin.get();
        return 0;
    }
    Output:
    Code:
    (16-20)
    Destructor called on object 3.
    Sent from my iPadŽ

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