C++这(在这种情况下到当前班级的链接是什么)
因此:
struct A { void foo(int) { } };
typedef std::function<void(int)> Function;
typedef std::vector<Function> FunctionSequence;
typedef FunctionSequence::iterator FunctionIterator;
FunctionSequence funcs;
A a;
funcs.push_back(std::bind(&A::foo, &a, std::placeholders::_1));
funcs.push_back(std::bind(&B::bar, &b, std::placeholders::_1));
// this calls a.foo(42) then b.bar(42):
for (FunctionIterator it(funcs.begin()); it != funcs.end(); ++it)
(*it)(42);
如果我们在类 A
中订阅 funcs.push_back
,我们会说而不是 &a
this
So having:
struct A { void foo(int) { } };
typedef std::function<void(int)> Function;
typedef std::vector<Function> FunctionSequence;
typedef FunctionSequence::iterator FunctionIterator;
FunctionSequence funcs;
A a;
funcs.push_back(std::bind(&A::foo, &a, std::placeholders::_1));
funcs.push_back(std::bind(&B::bar, &b, std::placeholders::_1));
// this calls a.foo(42) then b.bar(42):
for (FunctionIterator it(funcs.begin()); it != funcs.end(); ++it)
(*it)(42);
If we were inside class A
subscribing funcs.push_back
would we say instead of &a
this
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解你的问题,答案应该是肯定的。从通过
variable
调用的实例方法来看,&variable
始终等于this
。If I understood correctly your question, the answer should be yes.
&variable
is always equal tothis
as seen by the instance methods called overvariable
.是的,这听起来很合乎逻辑,但这只是一个猜测。
从
A
内部订阅,您是否希望将回调存储到A
的这个特定实例。如果是,那么您需要这个
。我们不知道您的需求,我可以想象所有三种变体(
&a
、&b
或this
)的情况正确的。yes, it sounds logical, but it's just a guess.
subscribing from inside
A
, would you like to store callback to this particular instance ofA
. If yes then you needthis
.we don't know your needs, and I can imagine cases where all three variants (
&a
,&b
orthis
) are correct.