I have the following code
#include <iostream>
struct mystruct
{
float x;
};
void fun(mystruct* ptr)
{
mystruct str = {10};
ptr = &str;
std::cout<<"bbq";
}
class cls
{
public:
mystruct* ptr;
void fun()
{
mystruct str = {10};
ptr = &str;
std::cout<<"bbq";
}
};
int main()
{
mystruct* ptr = new mystruct;
fun(ptr);
std::cout<<"bbq";
cls obj;
obj.fun();
std::cout<<"bbq";
}
At first pointer ptr is set in function fun to address of local structure. When function returns local structure gets destroyed as expected.
But then there is method in class that does the same with member pointer, but after method retrurns and im back in main member pointer is still set. Aren't method local variables destroyed after method returns ?