【问题标题】:Using Boost statechart, how can I transition to a state unconditionally?使用 Boost 状态图,如何无条件转换到状态?
【发布时间】:2011-10-12 18:03:45
【问题描述】:

我有一个状态A,一旦A 的构造函数完成,我想无条件地转换到下​​一个状态B。这可能吗?

我尝试从构造函数发布一个事件,但它不起作用,即使它编译。谢谢。

编辑:这是我迄今为止尝试过的:

struct A : sc::simple_state< A, Active >
{
    public:
        typedef sc::custom_reaction< EventDoneA > reactions;
        A()
        {
            std::cout << "Inside of A()" << std::endl;
            post_event( EventDoneA() );
        }

        sc::result react( const EventDoneA & )
        {
            return transit< B >();
        }
};

这会产生以下运行时断言失败:

Assertion failed: get_pointer( pContext_ ) != 0, file /includ
e/boost/statechart/simple_state.hpp, line 459

【问题讨论】:

    标签: c++ boost boost-statechart


    【解决方案1】:

    我已经用我的解决方案更新了我的 OP,在这里我可以结束这个问题。

    问题是你不能从simple_state继承来实现想要的转换,你必须从state继承,这需要你相应地修改构造函数。

    struct A : sc::state< A, Active >
    {
        public:
            typedef sc::custom_reaction< EventDoneA > reactions;
            A( my_context ctx ) : my_base( ctx )
            {
                std::cout << "Inside of A()" << std::endl;
                post_event( EventDoneA() );
            }
    
            sc::result react( const EventDoneA & )
            {
                return transit< B >();
            }
    };
    

    【讨论】:

    • 你不应该用正确的答案更新问题,只回答问题。人们喜欢看原始问题。
    • @GregorBrandt - 我的错误,我已将 OP 恢复为我原来的问题(我认为),这应该可以解决所有问题。谢谢!
    • 那么,基本上,您忘记了上下文?明确指出进行了哪些更正总是有帮助的。
    • @Xeo 好点!上下文是必要的,因为我错误地从simple_state 继承,而您需要从state 继承。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2017-10-05
    相关资源
    最近更新 更多