I am trying to create a static map declared in the constructor of my class.
This map is to be initialized and filled with data in one method and free’d in another method.
Is this the correct way to do it?
using namespace std;
#include <map>
struct a {
string b;
string c;
}
class aClass:public myClass
{
public:
aClass();
virtual ~aClass();
private:
map<string, a> myMap;
void method(int a);
void amethod(int b);
}
void aClass::method(int a)
{
myMap = new map<string, a>;
// Addition of elements;
}
void aClass::amethod(int b)
{
// retrival of elements
myMap.clear();
delete myMap;
}
Here
myMapis not a pointer, so the initialization withnewis incorrect. Perhaps you are looking for:to copy into
myMapa default initialized map.Note that you don’t need (and in fact can’t)
delete myMap, as is not a pointer. It’s a member variable, and the compiler will take care of automatically destroying it when your class is destroyed.