开发者

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

开发者 https://www.devze.com 2023-01-03 20:20 出处:网络
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?

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-&g开发者_C百科t;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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号