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;
}

Recommended Answers

All 4 Replies

Well once for erek and once for the copy of erek in the function call.

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

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.