but now with the code above when I return a reference and bind it to a const reference prolonging the life the program crashes(every time) but compiles fine
and also side note how come you can prolong the life of an object by making it a const reference but not do the same with pointers?
I am not saying I am the best at this, but I will give it a try.
As I understand this in the second example "foo" is returning a reference to variable "f" which goes out of scope and is destroyed when the function ends. So, back in main you are trying to use reference to a variable that no longer exists.
In main line 15 "const" does not affect the function in any way that I know of. it does say that you can not change the value of "fo" in main.
In the function "foo" making "f" on line 8 "static" allowed the program to work.
Sorry if I am not as accurate as I should be, but that is what I see.