C++ this(在这种情况下,什么是指向当前类的链接)

标签 c++ oop c++11 this std

所以有:

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

最佳答案

如果我没有正确理解你的问题,答案应该是肯定的。 &variable 始终等于 this,正如通过 variable 调用的实例方法所见。

关于C++ this(在这种情况下,什么是指向当前类的链接),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4846093/

相关文章:

c++ - 手动 boost 序列化 multimap

php - OOP——我应该开发多少类?

c++ - 如何调试此 C++11 线程代码?

Perl:动态模块加载、对象继承和 "common helper files"

c++ - 在 C++ 迭代器问题中使用带有一对的映射作为键

c++ - 为什么没有检测到这种缩小转换?

python - 在 python 而不是 C++ 中替代读取二进制文件

c++ - 实例枚举的 WBEM OpenPegasus 限制

python - 导入如何从 python 文件中使用 Boost.Python

C#接口(interface)类+继承VS纯抽象类