В следующей программе:
#include <string>
#include <deque>
#include <assert.h>
struct Foo {
// member var
const std::string *m_pstr;
// ctor
Foo (const std::string *pS) : m_pstr (pS) {}
// copy ctor
Foo (const Foo& other) : m_pstr (other.m_pstr) {}
// swap
void swap (Foo &other) { std::swap (m_pstr, other.m_pstr); }
// assignment operator
Foo& operator=(Foo tmp)
{
this->swap (tmp);
return *this;
}
// dtor
~Foo () {}
};
struct FooQueue {
// member var
std::deque<Foo> m_q;
void Enqueue (const Foo &f)
{
m_q.push_back (f);
assert (*(m_q.front().m_pstr) == std::string("Hello")); // This Passes
}
void Enqueue (const std::string &s)
{
Foo f (&s);
Enqueue (f);
assert (*(m_q.front().m_pstr) == std::string("Hello")); // This Passes
}
};
void ProcessEvent (FooQueue &fq)
{
fq.Enqueue ("Hello");
assert (*(fq.m_q.front().m_pstr) == std::string("Hello")); // This Fails
}
int main (int argc, char* argv[])
{
FooQueue fq;
ProcessEvent (fq);
return 0;
}
утверждение в функции ProcessEvent () терпит неудачу, и я не знаю почему. Я ожидаю, что строковый литерал «Hello» в аргументе fq.Enqueue () сохранится через изменения в области видимости (из-за this), и я ожидал, что указатель члена m_pstr также продолжит указывать на этот строковый литерал через изменения в области видимости. Может кто меня просветить?
const char[6]
сохраняется, но неявно преобразованное rvaluestd::string
не сохраняется. - person PeterT   schedule 19.11.2014