0

I am using Qt5 on a Windows 7 platform.
I've implemented a Singleton for a database I work with.
So far it's ok, it works fine, but when I compile the code I always get 2 warnings related to copy constructor and to assignment operator.

Here is the code:

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;
};

And here are the compiler warnings:

1) Base class QObject should be explicitly initialized in the copy constructor
2) No return statement in function returning non-void (that's for the assignment operator code line).

Well, what can I do in order to finally get rid of these 2 warnings?

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68

1 Answers1

1
  1. Try to initialize QObject base with the same parent as other has:

    DataBase(DataBase const& other) :
    QObject(other.parent())
    // copy-construct members
    {
    } 
    
  2. The operator= should look like:

    DataBase &operator=(DataBase const& other)
    {
        QObject::operator=(other);
        // copy-assign members
        return *this;
    }
    

    The warning is about that you forgot to return *this;.

Note that what you're doing are not default implementations. They do nothing!

You'll probably want to use default keyword (if you're compiling under C++11 or later) to leave the implementation of these function up to the compiler:

DataBase(DataBase const &) = default;
DataBase &operator=(DataBase const&) = default;
Community
  • 1
  • 1
LogicStuff
  • 19,397
  • 6
  • 54
  • 74