过去我总是创建这样的 map :
class TestClass
{
private:
std::map<int,int> *mapA;
};
TestClass::TestClass
{
mapA = new std::map<int,int>();
}
TestClass::~TestClass
{
mapA->clear(); // not necessary
delete mapA;
}
所以,现在我在 Stackoverflow 上到处阅读:尽可能避免指针
目前我想创建没有指针和 new 的映射(不需要自己删除对象,并且内存泄漏的危险较小)!
class TestClass
{
public:
TestClass() : mapA() // this is also needed?
{};
private:
std::map<int,int> mapA;
};
正确创建 map 所需的任何其他步骤?
感谢您的帮助和/或澄清!
最佳答案
不,就是这样,您不需要在构造函数中显式初始化它。
关于c++ - 正确使用 std::map 作为类成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19045922/