如何从包装在类中的成员访问包装器类的成员
How to access members of wrapper class from member which is wrapped in class
例如,我得到了:"包装机.h"
class wrapper : public QWidget
{
Q_OBJECT
public:
Wrapped_class m_class;
private:
QTimer* m_timer;
}
"Wrapped_class.h"
class Wrapped_class
{
public:
Wrapped_class();
public slots:
f(); // slot which is called when m_timer send signal timeout()
}
"Wrapped_class.cpp"
Wrapped_class::Wrapped_class()
{
QOBject::connect(wrapper::m_timer, SIGNAL(timeout()), this, SLOT( f()))
}
我收到错误,包装器::m_timer无法访问
您需要对类进行pointer
或reference
才能访问其non static
成员。在包装时将pointer
传递给包装的类
将这样的东西添加到您的Wrapped_class
:
void Wrapped_class::setWrapper(wrapper *w)
{
m_wrapper = w;
}
并在包装对象时调用此函数。在构造函数中初始化m_rapper
以nullptr
根据您的意图和系统设计,您可以选择:
- 将"包装"类
- 的指针或引用传递给"包装"类。 请注意,您必须将包装类定义为
friend
才能访问private
成员。 - 编写"wrapper"类的成员函数来处理两个类之间的交互。(这并不真正符合您的限制,但它是一种设计替代方案。
m_timer
不是静态成员,因此您无法像那样访问它。在Wrapped_class.cpp
中,您需要包装类的实例才能使用它
除了wrapper::m_timer
不是静态的问题外,它也是私有的,这意味着Wrapped_class
无法访问它。您需要Wrapped_class
成为wrapper
的朋友,以便它访问私人成员。
相关文章:
- 如何将 c++ 类包装到 python 中,以便我可以使用 pybind11 访问其成员的公共方法(成员是一个对象指针)
- 使用 Swig 在成员中包装具有互斥锁的 C++ 类时出现问题
- c++\CLI dll包装器,用于调用c++类中的虚拟成员
- 在析构函数之后,围绕成员函数的C++lambda包装器是如何工作的
- 其他成员函数的通用"成员函数"包装器?
- 如何在 SWIG 中包装可变参数模板类的可变参数模板成员函数?
- std::带有指向成员函数和back_inserter的指针的转换包装器
- C 函数调用包装器包含类成员功能作为模板参数
- 使常规函数使用模板包装成员函数
- 将元组成员包装在其他模板化类型中
- CPP成员对C回调包装器的反馈
- OpenGL C++:VBO 的结构包装器不会初始化成员
- 使用可变参数模板(gcc、clang)的成员函数指针包装器
- 如何为其他类成员函数编写模板包装方法
- 模板化类包装成员函数调用的行为
- 如何使用模板函数参数编写包装函数,该功能可以采用超载的成员函数
- 使用struct作为成员功能的包装器
- 如何编写 C++ 类成员函数包装器
- 是否有一种方法可以让公共成员在班级外部无法解码,而无需访问包装器功能
- 推导成员函数包装器的返回类型时出错