I am quite new to oop in c++ and I came across the following piece of code while surfing the net:
#include<iostream>
using namespace std;
class cls
{int x;
public:
cls(int i=3) {x=i;}
int &f() const{ return x;}
};
int main()
{
const cls a(-3);
int b=a.f();
cout<<b;
return 0;
}
And when I try to run the code, it crashes due to the f function. Now I am not quite sure what is going on there and why it crashes, so I would need someone to enlighten me a little bit on the subject.