I have two classes, Article
and a subclass ArticleOnSale
(public inheritance).
And also a class Boutique
, which contains a vector<Article*> arts
.
I have defined the <<
operator for both Article
and ArticleOnSale
In Articles.cpp
I have a public Addarticle
void Addarticle(Article* a)
{
Article* p;
if (typeid(*a) == typeid(Article))
{
p = new Article(a);
arts.push_back(p);
cout << "\narticle\n";
}
else
{
p = new ArticleEnSolde(static_cast<const ArticleEnSolde&>(*arts[arts.size()])); //creates a huge problem
arts.push_back(p);
}
}
When I want to print out the articles, it only uses the <<
of Article
for both Article
and ArticleOnSale
.
ostream & operator<<(ostream& n, Boutique a)
{
n<<"\nshowing boutique:\n";
Article* p;
for (int i = 0; i < a.arts.size(); i++)
{
p = a.arts[i];
if (typeid(*a.arts[i]) == typeid(Article))
{
n << *a.arts[i];
n << "\narticle not on sale detected\n";
}
else
{
n << "i can detect article on sale";//just debugging (doesn't work)
}
}
return n;
}