I'm working on a Boost Spirit Qi project that uses phoenix::construct
to create an object that has a pointer to another object. I noticed that using phoenix::construct
calls the destructor at some point (I'm guessing at the end of the function). Why does the destructor get called? How do I get around this?
Note: this isn't my actual code, I made a toy example so it wouldn't be so long
Objects
class Bar
{
public:
Bar(int y)
{
this->x = y;
}
~Bar()
{
}
int x;
};
class Foo
{
public:
Foo()
{
}
Foo(Bar* bar) : _bar(bar)
{
}
~Foo()
{
delete this->_bar;
}
void DoStuff()
{
std::cout << this->_bar->x << std::endl;
}
private:
Bar *_bar;
};
Grammar
template <typename Iterator>
struct TestGrammar : qi::grammar < Iterator, Foo(), ascii::space_type >
{
TestGrammar() : TestGrammar::base_type(foo)
{
foo = bar[qi::_val = phoenix::construct<Foo>(qi::_1)];
bar = qi::double_[qi::_val = phoenix::new_<Bar>(qi::_1)];
}
qi::rule < Iterator, Foo(), ascii::space_type > foo;
qi::rule < Iterator, Bar*(), ascii::space_type> bar;
};
Calling Code
std::getline(std::cin, string);
iter = string.begin();
end = string.end();
bool result = qi::phrase_parse(iter, end, grammar, space, f);
if (result)
{
State s;
f.DoStuff();
}
else
{
std::cout << "No Match!" << std::endl;
}