无法理解c++中处理程序的概念
Unable to understand this concept of handlers in c++
我正在编写一段代码,突然发现了一些新的东西。然而,为了更好地理解,我尝试编写自己的代码。
#include<iostream>
using namespace std;
class material
{
public:
material()
{
cout<<"material() called"<<endl;
}
bool test_func()
{
cout<<"Hello World"<<endl;
return true;
}
};
class server
{
private:
material *mat;
public:
server()
{
cout<<"server() called"<<endl;
}
material *matrl()
{
return mat;
}
};
class Handler
{
public:
Handler()
{
cout<<"Handler() called"<<endl;
}
server svr;
bool demo()
{
bool ret;
ret=svr.matrl()->test_func();
return ret;
}
};
int main()
{
Handler h;
cout<<"returned by demo():"<<h.demo()<<endl;
return 0;
}
甚至我也得到了想要的输出,那就是:
server() called
Handler() called
Hello World
returned by demo():1
但我无法理解这里的某些概念:
material *matrl()
{
return mat;
}
函数调用
ret=svr.matrl()->test_func();
这是如何运作的,这背后的概念是什么?有人能帮我吗???
如果重写,可以避免混淆
material *matrl()
{
return mat;
}
至
material* matrl()
{
return mat;
}
两者都一样。这是一个返回指向材料类型对象的指针的函数
现在
ret=svr.matrl()->test_func();
因为CCD_ 1返回一个指向成员函数需要使用->的对象的指针。或者*(svr.matr1()).test_func();
相关文章:
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 为什么我的 IExtractIcon 处理程序没有被调用?
- 在遍历处理程序的向量时注册和注销处理程序
- 有可能在信号处理程序中设置promise吗
- 在信号处理程序中捕获C++未处理的异常并恢复应用程序
- 在C++程序中使用的迭代器中未处理的异常
- 有关图像处理应用程序的硬件和软件安全性的建议
- 通过安装信号处理程序关闭多线程应用程序
- QDateTime::toString() 在退出处理程序中使用时失败
- 如何在 WindowProc 处理程序中区分箭头键和数字键盘?
- async_write完成处理程序最早何时完成?
- C++事件系统 - 多态事件和事件处理程序
- 使用信号处理程序处理从 FIFO 接收的数据
- 使用独立的 c++ 程序处理用 C 编写的字符设备驱动程序
- C 程序处理两个文件,而不是一个文件
- 是什么让这个程序处理缓慢?(C++)
- QT 中的应用程序>处理消息?
- MongoDB C++驱动程序处理副本集连接故障
- 使用 OpenCV 程序处理多页 PDF