Q_Property and reference data type
-
Hi,
I try to start with Qt's property mechanism.
The following is a first attemp
#ifndef DBCONTEXT_H #define DBCONTEXT_H #include "dbset.h" #include "dokumententity.h" #include "qsqldatabase.h" #include "quoteentity.h" #include <QObject> class DBContext : QObject { Q_OBJECT public: DBContext(); ~DBContext(); Q_PROPERTY(DBSet<DocumentEntity> Documents READ Documents CONSTANT) DBSet<DocumentEntity> Documents() const; private: DBSet<DocumentEntity> m_Documents; }; #endif // DBCONTEXT_Hand
DBSet<DocumentEntity> DBContext::Documents() const { return m_Documents; }this seems to work, but...
... should it be a better way to give reference type to avoid unnecessary copying?
DBSet<DocumentEntity> &DBContext::Documents() const { return &m_Documents; }And this fails with
dbcontext.cpp:29:62: Non-const lvalue reference to type 'DBSet<DocumentEntity>' cannot bind to a temporary of type 'const DBSet<DocumentEntity> *'I am confused since the lvalue is not taken as const as in the declaration.
C++ const behaviour is still some miracle for me.
Any hints, how to deal with it? And how should Q_PROPERTY look like to deal with reference types?
greets
Joachim
-
Hi,
I try to start with Qt's property mechanism.
The following is a first attemp
#ifndef DBCONTEXT_H #define DBCONTEXT_H #include "dbset.h" #include "dokumententity.h" #include "qsqldatabase.h" #include "quoteentity.h" #include <QObject> class DBContext : QObject { Q_OBJECT public: DBContext(); ~DBContext(); Q_PROPERTY(DBSet<DocumentEntity> Documents READ Documents CONSTANT) DBSet<DocumentEntity> Documents() const; private: DBSet<DocumentEntity> m_Documents; }; #endif // DBCONTEXT_Hand
DBSet<DocumentEntity> DBContext::Documents() const { return m_Documents; }this seems to work, but...
... should it be a better way to give reference type to avoid unnecessary copying?
DBSet<DocumentEntity> &DBContext::Documents() const { return &m_Documents; }And this fails with
dbcontext.cpp:29:62: Non-const lvalue reference to type 'DBSet<DocumentEntity>' cannot bind to a temporary of type 'const DBSet<DocumentEntity> *'I am confused since the lvalue is not taken as const as in the declaration.
C++ const behaviour is still some miracle for me.
Any hints, how to deal with it? And how should Q_PROPERTY look like to deal with reference types?
greets
Joachim
-
@MasterQ C++ does not allow overloading a function with different return types.
in your case, the two funcs are same, but return types are different.sry, maybe my post was misleading
class DBContext : QObject { Q_OBJECT public: DBContext(); ~DBContext(); Q_PROPERTY(DBSet<DocumentEntity> Documents READ Documents CONSTANT) Q_PROPERTY(DBSet<BillEntity> Bills READ Bills CONSTANT) DBSet<DocumentEntity> Documents() const { return m_Documents; } DBSet<BillEntity> &Bills() const { return &m_Bills; }; private: const DBSet<DocumentEntity> m_Documents; const DBSet<BillEntity> m_Bills; };The first version of the getter (Documents) returns a full copy of the DBSet. This is, to my opinion, not a good solution. The second getter (Bills) returns a reference. While syntax of version 1 is OK, the error given above is thrown for version 2 with reference type.
Question:
Why is the lvalue not recognized as const in version2? How to do it right?Another question: How to write the line with Q_PROPERTY to achieve a getter with returning reference type?
-
sry, maybe my post was misleading
class DBContext : QObject { Q_OBJECT public: DBContext(); ~DBContext(); Q_PROPERTY(DBSet<DocumentEntity> Documents READ Documents CONSTANT) Q_PROPERTY(DBSet<BillEntity> Bills READ Bills CONSTANT) DBSet<DocumentEntity> Documents() const { return m_Documents; } DBSet<BillEntity> &Bills() const { return &m_Bills; }; private: const DBSet<DocumentEntity> m_Documents; const DBSet<BillEntity> m_Bills; };The first version of the getter (Documents) returns a full copy of the DBSet. This is, to my opinion, not a good solution. The second getter (Bills) returns a reference. While syntax of version 1 is OK, the error given above is thrown for version 2 with reference type.
Question:
Why is the lvalue not recognized as const in version2? How to do it right?Another question: How to write the line with Q_PROPERTY to achieve a getter with returning reference type?
-
sry, maybe my post was misleading
class DBContext : QObject { Q_OBJECT public: DBContext(); ~DBContext(); Q_PROPERTY(DBSet<DocumentEntity> Documents READ Documents CONSTANT) Q_PROPERTY(DBSet<BillEntity> Bills READ Bills CONSTANT) DBSet<DocumentEntity> Documents() const { return m_Documents; } DBSet<BillEntity> &Bills() const { return &m_Bills; }; private: const DBSet<DocumentEntity> m_Documents; const DBSet<BillEntity> m_Bills; };The first version of the getter (Documents) returns a full copy of the DBSet. This is, to my opinion, not a good solution. The second getter (Bills) returns a reference. While syntax of version 1 is OK, the error given above is thrown for version 2 with reference type.
Question:
Why is the lvalue not recognized as const in version2? How to do it right?Another question: How to write the line with Q_PROPERTY to achieve a getter with returning reference type?
@MasterQ said in Q_Property and reference data type:
DBSet<BillEntity> &Bills() const { return &m_Bills; };
This is wrong: you're trying to return a pointer, not reference!
Should be:const DBSet<BillEntity> &Bills() const { return m_Bills; }; -
@MasterQ said in Q_Property and reference data type:
DBSet<BillEntity> &Bills() const { return &m_Bills; };
This is wrong: you're trying to return a pointer, not reference!
Should be:const DBSet<BillEntity> &Bills() const { return m_Bills; };dbcontext.cpp:29:62: Binding reference of type 'DBSet<...>' to value of type 'const DBSet<...>' drops 'const' qualifierI had tried this also before. But the main issue remains. Why is "const" dropped?
This is not simply a warning, it is an error!
--EDIT--
OK, I got you poltergeist!
I just deleted all and rewrote the whole thing new. Now it is working and no errors about dropping const appears anymore.
Just wasted a whole day by struggling with this bullsh*t
-
dbcontext.cpp:29:62: Binding reference of type 'DBSet<...>' to value of type 'const DBSet<...>' drops 'const' qualifierI had tried this also before. But the main issue remains. Why is "const" dropped?
This is not simply a warning, it is an error!
--EDIT--
OK, I got you poltergeist!
I just deleted all and rewrote the whole thing new. Now it is working and no errors about dropping const appears anymore.
Just wasted a whole day by struggling with this bullsh*t
@MasterQ said in Q_Property and reference data type:
Why is "const" dropped?
Because you were trying to return a non-const reference to a const object.