This simple block of code is behaving in an unexpected way.
#include <iostream>
using namespace std;
class Node
{
public:
char* data;
Node(char d)
{
data = &d;
}
};
int main()
{
Node NodeA = Node('c');
cout<<*(NodeA.data)<<endl;
return 0;
}
I was expecting to get 'c' as the output, but instead it outputs '}'. I had the feeling that it must be related to assigning the "data" pointer to an anonymous variable which is the 'c'.
I found this question discussing a similar issue.
But as it was mentioned in the top answer, the anonymous variable will only be killed if it was not bounded by a pointer referencing it by the end of the expression. Which is what I believe is not the case here as I am binding the pointer ("data") to it, but somehow it still gets killed.
I want to know what is going here that is causing the unexpected output.