I have a class X with a member std::map<int,int> m_lookupTable. Which of the following should I use :
Class X {
...
private:
std::map<int,int> m_lookupTable;
...
}
or allocate using new and delete in destructor of Class
class X{
private:
std::map<int,int>* m_lookupTable;
X() {
m_lookupTable = new std::map<int,int>();
}
~X(){
delete m_lookupTable;
}
}
What should be preferred way and why ?
newanddelete, and use containers and smart pointers instead. The only exception is when you're writing a custom container or smart pointer.