qGetPtrHelper在本例中的用途是什么?
What is the use of qGetPtrHelper in this example?
本文关键字:是什么 qGetPtrHelper 更新时间:2023-10-16
我正在阅读这篇文章,我看到了以下定义(在qglobal.h):
template <typename T> static inline T *qGetPtrHelper(T *ptr) { return ptr; }
template <typename Wrapper> static inline typename Wrapper::pointer qGetPtrHelper(const Wrapper &p) { return p.data(); }
#define Q_DECLARE_PRIVATE(Class)
inline Class##Private* d_func() { return reinterpret_cast<Class##Private *>(qGetPtrHelper(d_ptr)); }
inline const Class##Private* d_func() const { return reinterpret_cast<const Class##Private *>(qGetPtrHelper(d_ptr)); }
friend class Class##Private;
我知道宏为使用D-pointer/pImpl模式的类定义了公共函数。然而,我不太明白qGetPtrHelper
函数的必要性。它只是返回一个指针的副本,该指针将立即被强制转换。没有这个函数,ptr
变量就不能直接强制转换吗?
d_ptr可以是一个智能指针(例如,QScopedPointer),在这种情况下,它不能直接传递给reinterpret_cast: d_func()必须使用成员函数访问内部指针,因此需要两个版本的宏(事实上,在qGetPtrHelper存在之前,曾经有两个版本)。qGetPtrHelper所做的是在作为参数传递时触发对智能指针的隐式强制转换,从而消除对特殊处理的需要。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++中名称篡改的目的是什么
- 在 c++ 中拥有一组结构的正确方法是什么?
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 是什么阻止DOMTimerCoordinator::NextID进入无休止的循环
- 派生类销毁的最佳实践是什么
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 通过JNI传递数据数组的最快方法是什么
- "using namespace std;"在C++的作用是什么?
- 在两台机器之间进行时间戳的最佳c++chrono函数是什么
- 文件系统:复制功能的速度秘诀是什么
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 使用QQuickFramebufferObject时同步数据的最佳方式是什么
- 是什么原因导致它无法编译?它是声明签名还是在函数本身的实现中
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么