For this to understand, you should know about reference in C++.
see this simple code explains about reference,
using namespace std;
cout<<a<<endl; //output = 5
cout<<b<<endl; //output = 5
cout<<a<<endl; //output = 8
cout<<b<<endl; //output = 8
It shows that, reference variable(b) has access to the original variable(a).
Likewise, the code that you have posted, fun() has a return type (int&), which means it returns the reference of the x . And most important, the statement ,
fun() = 30; assigns 30 to the returned reference by fun(). and hence , the output is 30.
I AM THANKFUL TO YOU TO HAVE ASKED SUCH A QUESTION,AS IT HELPED ME TO LEARN FEW THINGS TOO. Please do ask , if there is any confusion.
Thank You. UP VOTE it if you are satisfied.