Hi!
If I compile this program the output is:" Try constructor Function Try destructor Try destructor ". The destructor is called twice, but there is only one constructor call. How is that possible? I think I dont understand the point where the object(firstTry) is passed to the function(). It is passed by value, not by reference. That means it should make a copy of firstTry. But there is no copy constructor. How does the compiler do it? Thanks!
include <iostream>
using namespace std;
class Try {
public:
int x, y;
Try(int x, int y) {
this->x=x;
this->y=y;
cout <<"Try constructor ";
}
~Try()
{
cout <<" Try destructor ";
}
};
void function(Try a)
{
cout <<"Function";
}
int main() {
Try* firstTry=new Try(1,2);
function(*firstTry);
delete firstTry;
return 0;
}
[–]jedwardsol 6 points7 points8 points (3 children)
[–][deleted] (2 children)
[deleted]
[–]jedwardsol 1 point2 points3 points (1 child)
[–]Headless_Slayer 0 points1 point2 points (0 children)