Reputation: 1123
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?
Upvotes: 3
Views: 9582
Reputation: 749
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).
Upvotes: 5
Reputation: 78280
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);
Upvotes: 6