unique_ptr operator overloading is disabled by QScopedPointer
-
In my example
QScopedPointer or std::unique_ptr disable operator overloading. Can I use my operator overloading with QScopedPointer or unique_ptr?class1 cl1; std::unique_ptr<class1> cl2; QScopedPointer<class1> cl3; QBuffer buffer; buffer.open(QIODevice::WriteOnly); QDataStream myStream(&buffer); myStream << cl1 << " " << cl2.get() << " " << cl3.get(); qDebug() << buffer.data();
output
"\x00\x00\x00\x0E\x00""d\x00""e\x00""f\x00""a\x00u\x00l\x00t\x00\x00\x00\x05 \x00\x00\x00\x00\x00\x05 \x00\x00"
I make full code example based on topic
-
The posted code will not compile for me (Linux, GCC13, Qt 6.7.2):
/tmp/unique_ptr_operator_overloading_unique_ptr/src/main.cpp: In function ‘int main(int, char**)’: /tmp/unique_ptr_operator_overloading_unique_ptr/src/main.cpp:15:42: error: use of deleted function ‘QDataStream& QDataStream::operator<<(const volatile void*)’ 15 | myStream << cl1 << " " << cl2.get() << " " << cl3.get(); | ^
uniqueptr::get() and QScopedPointer::get() return pointers and there is no suitable streaming operator according to my compiler.
Anyway, @VRonin is correct but you need to initialise cl2 and cl3 with valid pointers. Presently, cl2 and cl3 are wrappers on nullptrs, so deferencing these might be terminal.
class1 cl1; std::unique_ptr<class1> cl2 = std::make_unique<class1>(); QScopedPointer<class1> cl3(new class1); QBuffer buffer; buffer.open(QIODevice::WriteOnly); QDataStream myStream(&buffer); myStream << cl1 << " " << *cl2 << " " << *cl3; qDebug() << buffer.data(); return a.exec(); }
-
The posted code will not compile for me (Linux, GCC13, Qt 6.7.2):
/tmp/unique_ptr_operator_overloading_unique_ptr/src/main.cpp: In function ‘int main(int, char**)’: /tmp/unique_ptr_operator_overloading_unique_ptr/src/main.cpp:15:42: error: use of deleted function ‘QDataStream& QDataStream::operator<<(const volatile void*)’ 15 | myStream << cl1 << " " << cl2.get() << " " << cl3.get(); | ^
uniqueptr::get() and QScopedPointer::get() return pointers and there is no suitable streaming operator according to my compiler.
Anyway, @VRonin is correct but you need to initialise cl2 and cl3 with valid pointers. Presently, cl2 and cl3 are wrappers on nullptrs, so deferencing these might be terminal.
class1 cl1; std::unique_ptr<class1> cl2 = std::make_unique<class1>(); QScopedPointer<class1> cl3(new class1); QBuffer buffer; buffer.open(QIODevice::WriteOnly); QDataStream myStream(&buffer); myStream << cl1 << " " << *cl2 << " " << *cl3; qDebug() << buffer.data(); return a.exec(); }
-
-