Question

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?

Was it helpful?

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);

OTHER TIPS

Consider using std::shared_ptr instead QSharedPointer if you work with modern C++11 compiler (e.g. GCC 4.6 or above and MSVC 10.0).

First of all, it's a standard and second thing, you could use std::make_shared to init your pointer which can be faster! (For example, in MSVS2010/2012 allocation occurs only once for make_shared instead two allocations: one for new and one for internal counter).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top