高级类功能和C++;标准库,关于示例的一些问题 我很难理解C++的一些高级特性。 我试图为自己的信号/插槽系统编码,找到了一个示例并遇到了一些问题: Signal():当前的\u id\u(0){}-它是某种派生类吗?0在这里是什么意思 int connect_成员(T*inst,void(T::*func)(Args…){-T::“在这里做什么
高级类功能和C++;标准库,关于示例的一些问题 我很难理解C++的一些高级特性。 我试图为自己的信号/插槽系统编码,找到了一个示例并遇到了一些问题: Signal():当前的\u id\u(0){}-它是某种派生类吗?0在这里是什么意思 int connect_成员(T*inst,void(T::*func)(Args…){-T::“在这里做什么,c++,class,signals-slots,C++,Class,Signals Slots,可变标准::映射插槽-调用“插槽”时会发生什么 在这个例子中 #ifndef SIGNAL_HPP #define SIGNAL_HPP #include <functional> #include <map> // A signal object may call multiple slots with the // same signature. You can connect functions to the signal // which will be ca
可变标准::映射插槽
-调用“插槽”时会发生什么
在这个例子中
#ifndef SIGNAL_HPP
#define SIGNAL_HPP
#include <functional>
#include <map>
// A signal object may call multiple slots with the
// same signature. You can connect functions to the signal
// which will be called when the emit() method on the
// signal object is invoked. Any argument passed to emit()
// will be passed to the given functions.
template <typename... Args>
class Signal {
public:
Signal() : current_id_(0) {}
// copy creates new signal
Signal(Signal const& other) : current_id_(0) {}
// connects a member function to this Signal
template <typename T>
int connect_member(T *inst, void (T::*func)(Args...)) {
return connect([=](Args... args) {
(inst->*func)(args...);
});
}
// connects a const member function to this Signal
template <typename T>
int connect_member(T *inst, void (T::*func)(Args...) const) {
return connect([=](Args... args) {
(inst->*func)(args...);
});
}
// connects a std::function to the signal. The returned
// value can be used to disconnect the function again
int connect(std::function<void(Args...)> const& slot) const {
slots_.insert(std::make_pair(++current_id_, slot));
return current_id_;
}
// disconnects a previously connected function
void disconnect(int id) const {
slots_.erase(id);
}
// disconnects all previously connected functions
void disconnect_all() const {
slots_.clear();
}
// calls all connected functions
void emit(Args... p) {
for(auto it : slots_) {
it.second(p...);
}
}
// assignment creates new Signal
Signal& operator=(Signal const& other) {
disconnect_all();
}
private:
mutable std::map<int, std::function<void(Args...)>> slots_;
mutable int current_id_;
};
#endif /* SIGNAL_HPP */
\ifndef信号\u水电站
#定义信号_水电站
#包括
#包括
//一个信号对象可以使用
//相同的签名。您可以将函数连接到信号
//在
//已调用信号对象。传递给emit()的任何参数
//将传递给给定函数。
模板
类信号{
公众:
Signal():当前_id_u0{}
//复制创建新信号
信号(信号常数和其他):当前id(0){
//将成员函数连接到此信号
模板
int connect_成员(T*inst,void(T::*func)(Args…){
返回连接([=](Args…Args){
(inst->*func)(args…);
});
}
//将常量成员函数连接到此信号
模板
int connect_成员(T*inst,void(T::*func)(Args…)const){
返回连接([=](Args…Args){
(inst->*func)(args…);
});
}
//将std::函数连接到信号。返回
//值可用于再次断开该功能
int connect(标准::函数常量和插槽)常量{
插槽插入(std::make_pair(++当前_id_uu,插槽));
返回当前\u id\u;
}
//断开先前连接的功能
无效断开连接(内部id)常量{
插槽擦除(id);
}
//断开以前连接的所有功能
void disconnect_all()常量{
插槽清除();
}
//调用所有连接的函数
无效发射(Args…p){
用于(自动it:插槽){
第二(p…);
}
}
//分配产生新的信号
信号和操作员=(信号常量和其他){
断开所有连接();
}
私人:
可变std::映射槽;
可变的int当前\u id;
};
#endif/*信号_水电站*/
它叫什么名字,这样我就可以自己找了。
我找到了一个过于复杂的例子吗
谢谢。按顺序回答您的问题:
Signal():当前\u id\u(0){}
Signal
的构造函数中的初始化器列表。它告诉编译器当Signal
对象使用该构造函数构造时,当前的\u id\u
应该初始化为0
int connect_成员(T*inst,void(T::*func)(Args…){
T::
是一个范围解析,而T:*
专门指一个成员函数。函数调用connect\u member
期望接收一个指向-T
的指针,然后是指向-T
的成员函数的指针(该函数接受未指定的参数,并且不返回任何内容)
mutable std::map slot_;
插槽
是信号
的数据成员,类型为映射
从整数到返回空值的函数。可变
关键字告诉编译器,即使拥有的信号
为常量,也可以更改插槽
(例如,在声明为const
)的成员函数中。您是“小的”关于语法的问题是C++书籍中的丰富部分。选择一个并学习。每个问题的问题太多,请把数量限制在一个。尽管回答了这个问题,我也同意上面的和接近的投票。你问了几个中层语法。这是很好的东西,但应该是A。搜索(可能存在重复);然后b)在单独的问题中提问。