64,654
社区成员
发帖
与我相关
我的任务
分享
保存A*类型的指针,例如
list<A*> alist;
A* pb = new B();
A* pc = new C();
alist.push_back(pb);
alist.push_back(pc);
class A
{
public:
void Show(std::string str)
{
std::cout<<str<<std::endl;
};
bool operator == (const A& a)
{
if(this == &a) return true;
return false;
};
};
class B : public A{};
class C : public A{};
class H
{
private:
std::list<A*> list;
public:
void Add(A* a)
{
if(std::find(this->list.begin(), this->list.end(), a) == this->list.end())
{
this->list.push_back(a);
}
}
void Show(std::string str)
{
std::list<A*>::iterator it = this->list.begin();
while(it != this->list.end())
{
(*it)->Show(str);
it++;
}
}
};
int main()
{
std::string str;
std::cin>>str;
B* b = new B();
C* c = new C();
H h;
h.Add(b);
h.Add(c);
h.Add(b);
h.Add(b);
h.Add(b);
h.Show(str);
delete b;
delete c;
std::cin>>str;
return 0;
}