Reference to a pointer question

Posted by Yogesh Arora on Stack Overflow See other posts from Stack Overflow or by Yogesh Arora
Published on 2010-04-01T13:58:51Z Indexed on 2010/04/01 14:03 UTC
Read the original article Hit count: 263

Filed under:

Please refer to the code below. In this code i am storing the const char* returned by test.c_str() into a reference. My question is Will the data be correctly refering to the contents of test. I am thinking that ptr returned by test.c_str() will be a temporary and if i bound it to a reference that reference will not be valid. Is my thinking correct

class RefPtrTest
{
    std::string test;
    StoringClass storingClass;
public: 
    RefPtrTest(): test("hello"), storingClass(test.c_str())
    {
    }
}

where StoringClass is

class StoringClass 
{
    const char*& data;
public: 
    StoringClass (const char*& input): data(input)
    {
    }
}

© Stack Overflow or respective owner

Related posts about c++