Re: Pimpl idiom without dynamic memory allocation
In article <47165e09$0$26728$426a74cc@news.free.fr>,
Michael DOUBEZ <michael.doubez@free.fr> wrote:
>Daniel Lidström a écrit :
>> In article <4716585e$0$25087$426a74cc@news.free.fr>,
>> Michael DOUBEZ <michael.doubez@free.fr> wrote:
>>
>>> Daniel Lidström a écrit :
>>>> // create the pimpl instance without using new
>>>> Line::Line(const std::string& s) : m_pimpl(LineImpl(s)) {}
>>> Your local is destroyed when going out of scope. Doesn't it ?
>>
>> No it isn't. It is actually ok to bind a temporary object to a const
>> reference. There will be no "dangling" reference.
>
>It is ok to bind it but that doesn't mean the lifetime of the temporary
>is extended.
>
>Example:
>const std::string& foo()
>{
> return std::string("bar");
>}
>
>The value returned by foo() is an dangling reference.
>
Euh, it's not what he is doing, it's more like:
std::string foo()
{
return std::string("bar");
}
int main()
{
std::string const & val = foo();
....
|