开发者

C++ this (what would be a link to current class in such situation)

开发者 https://www.devze.com 2023-02-07 08:25 出处:网络
So having: struct A { void foo(int) { } }; typedef std::function<void(int)>开发者_JAVA技巧Function;

So having:

struct A { void foo(int) { } }; 

typedef std::function<void(int)>   开发者_JAVA技巧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


If I understood correctly your question, the answer should be yes. &variable is always equal to this as seen by the instance methods called over variable.


yes, it sounds logical, but it's just a guess.

subscribing from inside A, would you like to store callback to this particular instance of A. If yes then you need this.

we don't know your needs, and I can imagine cases where all three variants (&a, &b or this) are correct.

0

精彩评论

暂无评论...
验证码 换一张
取 消