gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:34:35 26 4
gpt4 key购买 nike

我有一个问题,关于如何通过映射对识别对象,实例化用该对识别的类型的对象,然后将它存储在某种容器(可能是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com