LessonInterface
class ILesson
{
public:
virtual void PrintLessonName() = 0;
virtual ~ILesson() {}
};
stl container
typedef list<ILesson> TLessonList;
calling code
for (TLessonList::const_iterator i = lessons.begin(); i != lessons.end(); i++)
{
i->PrintLessonName();
}
The error:
Description Resource Path Location Type passing ‘const ILesson’ as ‘this’ argument of ‘virtual void ILesson::PrintLessonName()’ discards qualifiers
Use
iterator
instead ofconst_iterator
or makePrintLessonName()
const function:You can't "put" objects of a class that has pure virtual functions(because you can't instantiate it). Maybe you mean:
OK, as others pointed out, you have to make
PrintLessonName
aconst
member function. I would add that there is another small pitfall here.PrintLessonName
must beconst
in both thebase
and thederived
classes, otherwise they will not have the same signature:To be honest, I find Jerry Coffin's answer helpful for redesigning the printing functionality.