C++ Interfaces in stl::list

c++

Solution

You can't "put" objects of a class that has pure virtual functions(because you can't instantiate it). Maybe you mean:

// store a pointer which points to a child actually.
typedef list<ILesson*> TLessonList;

OK, as others pointed out, you have to make `PrintLessonName` a `const` member function. I would add that there is another small pitfall here. `PrintLessonName` must be `const` in both the `base` and the `derived` classes, otherwise they will not have the same signature:

class ILesson
{
public:
    virtual void PrintLessonName() const = 0;
    virtual ~ILesson() {}
};


class SomeLesson : public ILesson
{
public:
    // const is mandatory in the child
    virtual void PrintLessonName() const
    {
        //
    }
    virtual ~SomeLesson() {}
};

To be honest, I find Jerry Coffin's answer helpful for redesigning the printing functionality.

Problem

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

Original source