Is the following code problamatic regarding inserting into a list an object that inherits from two classes?
class A
{
}
class B
{
}
class C : public A, public B
{
}
C *myObj = new C();
std::list<A*> myList;
myList.push_front(myObj);
Is creating a list of type A and inserting an object of type C which is part of type B problematic?
I know this code compiles but I am affrade of memory issues.
If its a problem, what other options do I have to solve this?
As long as the list stores the data by reference or pointer and the destructor is virtual you’re fine.
The basic problem is that you are not allowed to store a C into a variable of
A, but you can store it intoA&orA*. SoA a = C()would be just as bad as storing a C into alist<A>orvector<A>. This would lead to slicing