On the code below, do I need to create an instance of my list when I use the constructor in ClassWithList?
Also, why is addToList wrong? How can I achieve that?
class AbstClass;
class ObjClass : AbstClass;
class ClassWithList
{
std::list<AbstClass*> absts;
public:
ClassWithList(); //new list?
void addToList(ObjClass& obj)
{
list.push_back(obj);
}
};
You have to do some simple corrections (explanations in comments):
class AbstClass;
class ObjClass : public AbstClass; // << You need to derive as public
class ClassWithList
{
std::list<const AbstClass *> absts;
public:
ClassWithList(); // You don't need new list, it will be allocated
// already correctly at construction
void addToList(const AbstClass& obj)
{
absts.insert(&obj); // << pass the address
// ^^^^^ The member is named absts
}
};