I am learning C++. I use both visual studio 2015 and codeblocks IDE to code C++. I tried to write a program that returns a reference variable from a function and I get different results(two results) from 2 IDE( Visual Studio 2015 and codeblocks) although I run the same code. I tried writing the following code:
#include <iostream>
using namespace std;
class Demo
{
public:
int a;
};
//I wrote a function that returns a reference variable
Demo& func()
{
Demo temp;
temp.a = 1;
return temp;
}
int main()
{
Demo& d = func(); //it error if I run this code on Codeblocks and it run
//smoothly if I run it on Visual Studio 2015
cout<<d.a;
return 0;
}
I known that it depends on compiler but I want to know Which is correct in this case? Thank in advance!