尝试在映射C++中存储指向抽象类的指针
Trying to store a pointer to an abstract class in a map C++
我有一个程序,它将指向抽象类的指针存储到map中,以便我可以使用map中的数据来调用抽象类的具体类(使用工厂方法)。
它需要存储一个指针,因为类是抽象的,但问题是当我做一个指针时,我得到错误
没有匹配的成员函数来调用"insert"
我的代码如下所示:
形状工厂类标头
#ifndef ShapeFactoryManager_hpp
#define ShapeFactoryManager_hpp
#include <stdio.h>
#include "ShapeFactory.h"
#include "CircleFactory.h"
#include "PolygonFactory.h"
#include "LineFactory.h"
#include <map>
class ShapeFactoryManager
{
public:
ShapeFactoryManager();
const static ShapeFactoryManager& getInstance();
ShapeFactory createFactory(unsigned long shapeID) const;
bool RegisterShape(unsigned long id, ShapeFactory* factory) const;
private:
const std::map<unsigned long, ShapeFactory*>registrationTable;
};
#endif /* ShapeFactoryManager_hpp */
形状工厂经理.cpp
bool ShapeFactoryManager::RegisterShape(unsigned long id, ShapeFactory* factory) const
{
registrationTable.insert(std::make_pair(id, factory));
return true;
}
错误发生在:
registrationTable.insert(std::make_pair(id, factory));
std::map::insert
是一个不是 const 成员函数。从逻辑上讲,这是非常合乎逻辑的,因为如果地图是恒定的,我们就无法添加或删除它。
删除 registrationTable
和 RegisterShape
的常量限定符。如果成员函数需要更改状态,它也不应该是常量。
相关文章:
- 如何将子类作为函数的参数传递给期望基类,然后将该对象传递到指向这些抽象类对象的指针向量中?
- 两个抽象类,派生自同一个基类.如何访问从一个抽象类到另一个抽象类的指针
- 删除指向抽象类的指针向量
- 如何将抽象类指针作为Q_PROPERTY?
- 为什么我可以取消引用指向抽象类的指针?
- 指向抽象类的指针数组:指向 nullptr 或不指向 nullptr (C++)
- C++ abort() 在函数内的抽象类对象指针调用上
- 抽象类和独特的指针
- 如何使用抽象类指针避免内存泄漏
- 如何从抽象类中删除指针
- 指向模板抽象类的指针向量
- 指针到抽象类的功能超载
- 使用抽象类时,如何正确删除指针
- 抽象类和唯一指针 c++ 错误
- 尝试在映射C++中存储指向抽象类的指针
- C++中的抽象类指针
- 如何将子类分配给函数中的抽象类指针
- 带有抽象类指针的c++对象副本
- 如何在c++中实现与抽象类指针向量的缓存一致性
- C++访问抽象类指针向量的子类的成员