im new to c++ and im trying to understand how the "new" keyword works in c++ and when to use it , so i made this little program to test and see how the heap and stack works
#include <iostream>
#include <string>
class Player
{
private:
const std::string name;
public:
Player() : name("unknown") {}
Player(const std::string i_name) : name(i_name) {}
const std::string GetName() const {return name;}
};
int main()
{
Player* e;
{
Player player("Redouane");
e = &player;
std::cout<<player.GetName()<<std::endl;
}
std::cout<<(*e).GetName()<<std::endl;
}
based on what i've learned , the memory space in the stack should be freed by the end of the scope , which means the second output must be "unknown" . BUT , in my case it's not , it gives me the same output
Redouane
Redouane