【发布时间】:2015-04-22 13:00:24
【问题描述】:
我正在开发一个 Boost Spirit Qi 项目,该项目使用 phoenix::construct 创建一个具有指向另一个对象的指针的对象。我注意到使用 phoenix::construct 在某些时候调用析构函数(我猜在函数的末尾)。为什么要调用析构函数?我该如何解决这个问题?
注意:这不是我的实际代码,我做了一个玩具示例,所以不会那么长
对象
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;
};
语法
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;
};
调用代码
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;
}
【问题讨论】:
-
在
DoStuff不要忘记nullptr-check_bar
标签: c++ boost boost-spirit-qi boost-phoenix