i gave the copy constructor in the below code just to know when will it be called..
#include<iostream>
using namespace std;
class counter
{
int val;
public:
~counter()
{
cout<<"destructor"<<endl;
}
counter(counter&)
{
cout<<"copy constructor called"<<endl;
}
counter():val(0)
{}
counter& operator++()
{
++val;
*this;
}
int getval()
{
return val;
}
};
int main()
{
counter i;
cout<<"the value is"<<i.getval()<<endl;
++i;
cout<<"the value is"<<i.getval()<<endl;
counter a=++i;
cout<<"the value is"<<i.getval()<<endl;
cout<<"the value is"<<a.getval()<<endl;
}
i am getting the wrong output:
the value is 0
the value is 1
copy constructor called
the value is 2
the value is 1628937800
destructor
destructor
if i comment the copy constructor i am getting the correct output:
the value is 0
the value is 1
copy constructor called
the value is 2
the value is 2
destructor
destructor
can someone pls help me!!