Qt5:如何消除这个Singleton的编译器警告
Qt5: How to get rid of the compiler warnings for this Singleton?
我在Windows7平台上使用Qt5
我已经为与我一起工作的数据库实现了Singleton
到目前为止还可以,它工作得很好,但当我编译代码时,我总是收到两个与复制构造函数和赋值运算符有关的警告。
这是代码:
class DataBase : public QObject
{
Q_OBJECT
public:
static DataBase * instance(QObject * parent = 0);
static void destroy();
//
QString openDataBaseConnection();
void closeDataBaseConnection(QString & connectionName);
private:
DataBase(QObject * parent);
~DataBase();
DataBase(DataBase const &){} // <- copy constructor
DataBase & operator = (DataBase const &){} // <- assignment operator
static DataBase * pInstance;
};
以下是编译器警告:
1) 基类QObject应在复制构造函数中显式初始化
2) 函数中没有返回非void的return语句(用于赋值运算符代码行)。
那么,我该怎么做才能最终摆脱这两个警告呢?
-
尝试使用与
other
相同的父级初始化QObject
库:DataBase(DataBase const& other) : QObject(other.parent()) // copy-construct members { }
-
operator=
应该看起来像:DataBase &operator=(DataBase const& other) { QObject::operator=(other); // copy-assign members return *this; }
警告是关于您忘记使用
return *this;
。
请注意,您所做的不是默认实现。他们什么都不做!
您可能希望使用default
关键字(如果您在C++11或更高版本下进行编译),将这些函数的实现留给编译器:
DataBase(DataBase const &) = default;
DataBase &operator=(DataBase const&) = default;
相关文章:
- C/C++编译器通常会删除重复的库吗
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- Win32编译器选项和内存分配
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ Singleton - Prevent ::instance() to variable
- C++,我收到一个无法理解的编译器错误
- 在线编译器中的分段C++没有打印消息
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C/C++预处理器是否可以检测一些编译器选项
- 是否有C++编译器选项允许激进地删除所有函数调用,并将参数传递给具有空体的函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我需要知道编译器如何在cpp中使用析构函数
- 编译器如何区分std::vector的构造函数
- CLANG 编译器 说:变量"PTR"可能未初始化
- 告诉c++编译器该参数没有别名
- 使用仅使用一次的变量调用的复制构造函数.这可能是通过调用move构造函数进行编译器优化的情况吗
- Qt5:如何消除这个Singleton的编译器警告
- 在编译器中使用singleton
- VC++通用Singleton实现编译器错误