I commented the code for you and made it a bit more clear to read.
Read the comments from (1) -> (5).
#include <iostream>
using namespace std;
class A {
public:
A(int x) // (3): gets called by f to initialize an object of class A with x=1
{
a = x;
}
int a;
};
void fun(A temp) // (2): Assumes 1 is a type class A. But it hasn't yet been initialized. So it calls Constructor. (tmp = new A(1))
{
// (4): Now temp is an object of class A
cout << temp.a << endl; // Prints temp.a
// (5): temp object is getting destroyed.
}
int main()
{
fun(1); // (1): Calls fun() with argument of 1
}
solved Can anyone explain me how above code works and using which concept?