std::unique_ptr<QCanBusDevice> m_canDevice in another class
-
This somehow contradicts the idea of a unique pointer...
Why use a pointer as member here at all? -
@Christian-Ehrlicher said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:
This somehow contradicts the idea of a unique pointer...
first of all i use the QCanBus example of Qt .
Second I need only one device dynamically allocated -
@dziko147 said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:
Second I need only one device dynamically allocated
Again: why do you need to allocate it dynamically? And why do you think you need a unique_ptr then? Hoe you did not pass a parent to your QCanBusDevice then...
-
@dziko147 said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:
std::unique_ptr<QCanBusDevice> m_canDevice
you can return a raw pointer or a weak pointer. Be aware that you do not clean it up.
QCanBusDevice * getDevice()
{
return m_canDevice.get();
}As Christian pointed out, this is not a good design if a pointer is used somewhere else as well. Simply replace it with shared pointer(not a crime).
-
@dziko147 said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:
@Christian-Ehrlicher said in std::unique_ptr<QCanBusDevice> m_canDevice in another class:
This somehow contradicts the idea of a unique pointer...
first of all i use the QCanBus example of Qt .
Second I need only one device dynamically allocatedThese are not reasons for using unique pointer here. Learn more C++ basics. Your question is actually not a Qt one.
-
Hi
What should the "other" class use m_canDevice for ?
I mean MainWindow has all the slots etc so
could that other class simply not ask MainWindow to perform operations on its behalf instead of wanting access to its private member?