I'm new to C++ programming so this question might be basic, but here it is:
I have four classes - A, B, C and D. Their relationships are as defined below:
class B : public A;
class D : public C;
A is an abstract class (all methods are pure virtual). Class D implements a Print() function which C does not.
//Class D - some code
void Print()
{
//some code
}
Class A has an STL::list that holds pointers to objects of class C.
//Class A - some code
protected:
list<C*> myObjects;
In class B I have a function which pushes to myObjects pointers to objects of type D (again, D inherits C) which works perfectly.
Class B : public A
{
// Some code
D* obj = new D(...);
myObjects.push_back(obj);
return obj;
}
Finally, in class B I have a function that iterates over myObjects (which is inherited from class A) like so:
for(list<C*>::iterator it = myObjects.begin(); it != myObjects.end(); it++)
{
//I wish to call the function D.Print() but I get an error!
D *a = *it;
a->Print();
}
Error states:
error C2440: 'initializing': cannot convert from 'std::_List_iterator<_Mylist>' to 'D*'
I am under the impression that if "a" is a pointer to an object of class D, then if I give it the value of the pointer that is referenced by the iterator (which points to a pointer to an object of type D) I could call Print().
Can you help? Thanks in advance!