I found this code on the internet. I am a bit confused about the calling of the copy constructor and I just wanted to know why the copy constructor is called when the display function is called?
#include<iostream>
using namespace std;
class myclass
{
int val;
int copynumber;
public:
//normal constructor
myclass(int i)
{
val = i;
copynumber = 0;
cout<<"inside normal constructor"<<endl;
}
//copy constructor
myclass (const myclass &o)
{
val = o.val;
copynumber = o.copynumber + 1;
cout<<"Inside copy constructor"<<endl;
}
~myclass()
{
if(copynumber == 0)
cout<<"Destructing original"<<endl;
else
cout<<"Destructing copy number"<<endl;
}
int getval()
{
return val;
}
};
void display (myclass ob)
{
cout<<ob.getval()<<endl;
}
int main()
{
myclass a(10);
display (a);
return 0;
}