You cannot create an Interface
object since it's an abstract type. Even if Interface
were not abstract what you are trying would not work because of object slicing. Instead you need to create an array of Interface
pointers, e.g.
static Interface* const array[] = {
new Implementation(),
new ImplementationNumberTwo(),
new Implementation()
};
In C++ polymorphism only works through pointers (or references).
Of course using dynamic allocation to create the Interface
objects brings in new issues, like how those objects get deleted, but that's a separate issue.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…