c++ - 如何通过映射从派生类实例化对象

标签 c++ pointers dictionary polymorphism

我有一个问题,关于如何通过映射对识别对象,实例化用该对识别的类型的对象,然后将它存储在某种容器(可能是 vector )中。这里的问题是,我正在寻找的对象应该都是某个基类的派生类。

这是一个例子:

class BaseClass {
public:
    BaseClass() {cout << "BaseClass constructor...\n"};
    ~BaseClass() {cout << "BaseClass destructor...\n"};
};


class A : public BaseClass {
public:
    A() {cout << "A constructor...\n"};
    ~A() {cout << "A destructor...\n"};
};


class B : public BaseClass {
public:
    B() {cout << "B constructor...\n"};
    ~B() {cout << "B destructor...\n"};
};


class C : public BaseClass {
public:
    C() {cout << "C constructor...\n"};
    ~C() {cout << "C destructor...\n"};
};


int main(int argc, char *argv[])
{
    map <string, BaseClass*> my_map; // Map used to compare a string in order to identify the object type I'd like to make

    vector<BaseClass*> keyword_vct; // Vector to store the objects of the different derived class types

    BaseClass* ptr;
    my_map.insert (make_pair ("A", ptr = new A ));
    my_map.insert (make_pair ("B", ptr = new B));
    my_map.insert (make_pair ("C", ptr = new C));

    string testStr "B";

    map <string, BaseClass*> ::const_iterator it = my_map.find(testStr);
    if (it == oscards_map.end()) {
        cout << "String not found in map." << endl;
    }
    else {
        cout << it->first << "\t keyword found in map!" << endl;
        BaseClass* pSomekey;
        pSomekey = it->second; // This is where I'm lost

        keyword_vct.push_back(pSomekey); // Once I instantiate the derived object in the line above, I want to store it in a container.
    }
}   

所以我的主要问题是:

  1. 如何将 pSomekey 变成 ABC 类型的对象>?

  2. 如果我能够实例化其中一个派生类,我是否能够将这些不同类型的对象存储到同一个 vector 中,因为它们是 BaseClass 的派生类?

我注意到,当我为 map 制作对时,它们似乎在各自的派生类中构造了一个对象。

我还注意到,当执行 pSomekey = it->second; 时,没有构造任何对象。

请记住,这只是一个示例。在我的真实代码中,我将比较数百个 testStr 以生成数百个不同的对象。

如有任何帮助或建议,我们将不胜感激。谢谢!

最佳答案

  1. 在基类中添加一个成员函数来复制对象。
  2. 在派生类中适本地实现它们。
  3. 在需要时调用复制函数。

这是您的代码,已在正确的位置更新。

class BaseClass
    {
    public:
        BaseClass() {cout << "BaseClass constructor...\n"};
        ~BaseClass() {cout << "BaseClass destructor...\n"};
        virtual BaseClass* clone() const = 0;
    };


class A : public BaseClass
    {
    public:
        A() {cout << "A constructor...\n"};
        ~A() {cout << "A destructor...\n"};
        virtual A* clone() const { return new A();}
    };


class B : public BaseClass
    {
    public:
        B() {cout << "B constructor...\n"};
        ~B() {cout << "B destructor...\n"};
        virtual B* clone() const { return new B();}
    };


class C : public BaseClass
    {
    public:
        C() {cout << "C constructor...\n"};
        ~C() {cout << "C destructor...\n"};
        virtual C* clone() const { return new C();}
    };


int main(int argc, char *argv[])
    {

    map <string, BaseClass*> my_map; // Map used to compare a string in order to identify the object type I'd like to make

    vector<BaseClass*> keyword_vct; // Vector to store the objects of the different derived class types

    BaseClass* ptr;
    my_map.insert (make_pair ("A", ptr = new A ));
    my_map.insert (make_pair ("B", ptr = new B));
    my_map.insert (make_pair ("C", ptr = new C));

    string testStr "B";

    map <string, BaseClass*> ::const_iterator it = my_map.find(testStr);
    if (it == oscards_map.end()) {
        cout << "String not found in map." << endl;
        } else {
        cout << it->first << "\t keyword found in map!" << endl;
        BaseClass* pSomekey;
        pSomekey = it->second;

        // Make a copy of the object and store it in keyword_vct.
        keyword_vct.push_back(pSomekey->clone());
        }

    }   

关于c++ - 如何通过映射从派生类实例化对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27952742/

相关文章:

c++ - 用一行代码替换字符串中所有句点的 C++ 方法?

c++ - 我可以安全地将数字转换为枚举并带有后备值吗?

c# - => 和 get {} 之间的区别

javascript - 字典数据 ito d3 折线图 - 服务器端的 Django Python

c++ - 与其他语言相比,如何衡量 C 的 SNMP 性能?

c - 指针的数组表示法

c++ - 将指针传递给函数而不复制它

objective-c - 您可以通过编程方式在 Mac OS 的内置词典中查找单词吗?

python - 将字符串转换成字典

c++ - std::vector 中的重复元素