Accessing member of class through shared pointer

  c++, c++11

I am facing a issue, when I try to access members of class through shared pointer.
The program runs fine and gives expected output "Name: Michel". But when I comment line marked as COMMENT_THIS_LINE, it gives some junk value instead of Michel. Can anybody explain this behavior, and how can I get correct output without the marked line?

class TestShared{
public:
        TestShared(std::string name);
        std::string getName(void);
private:
        std::string m_name;
};

class Another{
public:
        const char *name;
};
TestShared::TestShared(std::string name) : m_name(name)
{
}
std::string TestShared::getName(void)
{
        return m_name;
}

std::shared_ptr<TestShared> allocate_ts()
{
        char p[] = "Michel";
        return std::make_shared<TestShared>(p);
}

Another GetAnother(std::shared_ptr<TestShared>  ts)
{
        Another an;
        std::string a = ts->getName();    //////////COMMENT_THIS_LINE
        an.name = ts->getName().c_str();
        return an;
}

int main()
{
        std::shared_ptr<TestShared>  ts = allocate_ts();
        Another an = GetAnother(ts);
        printf("Name: %sn", an.name);
        return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT