Search code examples
c++qtqsharedpointer

Setting QSharedPointer


I'm coming from a C# background and still getting my head around c++ and Qt smart pointers. This should be a basic question:

In myClass.h

QSharedPointer<AccessFlags> m_flags;

In myClass.cpp I'm trying to set (is set the correct word?) the m_flags pointer

if(m_flags.isNull())
    m_flags = new AccessFlags();


class AccessFlags{
public:
     QHash<QString,int> flags;
     AccessFlags();  //The dictionary is setup in the constructor
};

The compiler complains "no match for 'operator=' in.. How do I set the pointer?


Solution

  • You're trying to assign a raw pointer to a QSharedPointer in the line

    m_flags = new AccessFlags();
    

    You probably want something like

    m_flags = QSharedPointer<AccessFlags>(new AccessFlags);