test program, dont understand after calling function it goes two times destructor "-5"
is it wrong ?

#include <cstdlib>
#include <iostream>
using namespace std;
class calculation
{
      public:
             calculation();   // constructor
                         ~calculation();   // destructor.
};

  calculation::calculation()
  {  
            cout << "5"<<endl;
            }
  

calculation::~calculation()
{
           cout <<"-5"<<endl;
}
calculation function_one(calculation  addition);


          


int main(int argc, char *argv[])
{
  
    calculation erek;
    cout <<" calling function_one"<<endl;
    function_one(erek);
   
    system("PAUSE");
    return EXIT_SUCCESS;
}

// function oneeee.

calculation function_one(calculation addition)
{
    cout << "addition function_one returning "<<endl;
    return addition;
}

change this calculation function_one(calculation addition) to this calculation function_one(calculation& addition) and realize the difference.

I agree with both the answers. If you do not want destructor to be called twice then copy the object and see the difference.

change this calculation function_one(calculation addition) to this calculation function_one(calculation& addition) and realize the difference.

you both absolutely right. thanks

Edited 6 Years Ago by burcin erek: n/a

This question has already been answered. Start a new discussion instead.