gpt4 book ai didi

c++ - 在 boost scope exit 中调用对象函数

转载 作者:行者123 更新时间:2023-11-30 03:41:46 25 4
gpt4 key购买 nike

我有以下代码:如何在范围退出中调用成员函数。

class A
{
public:
void foo();
void bar();
};

void A::foo()
{
BOOST_SCOPE_EXIT(void)
{
bar(); // Does not compile
}
BOOST_SCOPE_EXIT_END
}

void A::bar() {}

void foo4()
{
A a;
a.foo();
}

错误信息

boost_scope_exit.cpp: In static member function ‘static void  
A::foo()::boost_se_guard_t_71::boost_se_body()’:
boost_scope_exit.cpp:73:13: error: cannot call member function ‘void
A::bar()’ without object
bar(); // Does not compile
^

如何从作用域导出调用成员函数。

在boost文档中找到答案:

Boost docs

只需搜索“捕获对象”

最佳答案

如果您将 bar 设为静态(并调用 A::bar()),您的代码将正常工作。
如果那不是一个选项,那么您似乎需要捕获 this 指针使用特殊符号 this_(注意结尾的下划线)。

BOOST_SCOPE_EXIT(this_)
{
this_->bar(); // note trailing underscore on this_
}
BOOST_SCOPE_EXIT_END

关于c++ - 在 boost scope exit 中调用对象函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37174252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com