I am trying to convert a project from c# to c++ and have come across a piece of functionality I am not sure on how to implement in c++.
I have a list (or vector) of objects, all casted to their parent type. In c# I can call a function from an object in this list without knowing the child and the appropriate child function will be called, however I am not sure how to make this certain functionality work in c++.
Code snippet from C#:
public void AddComponent(IComponent component)
{
Debug.Assert(component != null, "Component must not be null");
componentList.Add(component);
mask |= component.ComponentMask;
}
Retrieves the ComponentMask enum value from the Component and performs bitwise operation correctly.
Code snippet from C++:
void oEntity::AddComponent(IComponent &componentIn)
{
componentList.push_back(componentIn);
mask |= componentIn.ComponentMask();
}
This returns the error "IComponent cannot instantiate abstract class", if I remove the brackets from the method the operator overload no longer works and throws the error "binary '|=': no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)"
The mask value is an enum that contains bit shifted ints used as a flag to identify the component type. Operator has also been overloaded appropriately to work with the enum type.