Why am I able to create a copy constructor and overload the assignment operator for a QObject subclass?

StackOverflow https://stackoverflow.com/questions/3074518

  •  28-09-2019
  •  | 
  •  

Question

I was under the impression that QObject disabled the copy constructor and assignment operator... why am I able to compile this QObject derivative containing both of these?

#ifndef QVERSION_H
#define QVERSION_H

#include "silverlocklib_global.h"
#include <QtCore>

struct SILVERLOCKLIBSHARED_EXPORT QVersion : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool valid READ isValid)
    Q_PROPERTY(long major READ major)
    Q_PROPERTY(long minor READ minor)
    Q_PROPERTY(long build READ build)
    Q_PROPERTY(long revision READ revision)

public:
    QVersion(long major = 0, long minor = 0, long build = -1, long revision = -1, QObject *parent = NULL);
    QVersion(const QString &version, QObject *parent = NULL);
    QVersion(const QVersion &version);
    static QVersion parse(const QString& version, bool *ok = NULL);
    bool isValid() const;
    long compareTo(const QVersion &other) const;
    bool equals(const QVersion &other) const;
    QString toString() const;
    QString toString(int fieldCount) const;
    long major() const;
    inline int majorRevision() const { return (qint16)(this->m_revision >> 16); }
    long minor() const;
    inline int minorRevision() const { return (qint16)(this->m_revision & 65535); }
    long build() const;
    long revision() const;
    QVersion& operator=(const QVersion &version);
    friend bool operator==(const QVersion &v1, const QVersion &v2);
    friend bool operator!=(const QVersion &v1, const QVersion &v2);
    friend bool operator<(const QVersion &v1, const QVersion &v2);
    friend bool operator<=(const QVersion &v1, const QVersion &v2);
    friend bool operator>(const QVersion &v1, const QVersion &v2);
    friend bool operator>=(const QVersion &v1, const QVersion &v2);

private:
    inline static void copy(QVersion *const destination, const QVersion &source);
    static bool tryParseComponent(const QString &component, long *parsedComponent);

    long m_major;
    long m_minor;
    long m_build;
    long m_revision;
};

#endif // QVERSION_H
Was it helpful?

Solution

A derived class can always declare it's own copy constructor and assignment operator. If the base class disables it by making it private and leaving it undefined the derived class can't call the base class' version in its own implementation of course, but it can still implement them.

OTHER TIPS

Just as an addition to Omnifarious' answer. As he said you can implement the derived class' copy constructor, but you wouldn't be able to use the base class' copy constructor to initialize data members in base class.

If you don't specify which constructor to initialize your base class data member, the compiler will use base class' default constructor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top