您是否可以在标头中声明QScopedPointer:
QScopedPointer <T> _name;在.cpp定义/实例化中:
_name ( /*new T*/ );注意:我知道QScopedPointer没有执行此操作的运算符,只有一个ctor,但从概念上讲,这可以以某种方式实现吗?
发布于 2017-03-29 12:42:54
我们可以在类声明的头中使用
QScopedPointer<T>-typed类成员吗?
是。确保定义或声明了类型T:
///
/// File MyClass.h
///
// Either have:
#include "MyType.h" // defines MyType
// Or:
class MyType; // forward declaraion
class MyClass
{
public:
MyClass();
////
private:
QScopedPointer<MyType> m_pTypeObj;
};但是您应该始终在实例化对象并将指针存储在该QScopedPointer<MyType>中的位置定义类型
#include "MyClass.h" // defines MyClass
// If not through MyClass.h then must have:
#include "MyType.h" // defines MyType
MyClass::MyClass()
{
// now we can instantiate MyType
m_pTypeObj.reset(new MyType);
// and use the scoped pointer
m_pTypeObj->method();
}或者正如作者所暗示的那样:
MyClass::MyClass() : m_pTypeObj(new MyType)
{
// and use the scoped pointer
m_pTypeObj->method();
}这种方法也适用于现在可以取代QScopedPointer的std::unique_ptr。
https://stackoverflow.com/questions/43080236
复制相似问题