1

Hi!

C++ Problem!

I want to check (in C++) if a vector which contains objects of abstract type A contains objects of type B, where B is a class derived from A.

Why?

The reason I need this is that I'm trying to implement a basic Entity-Component System in C++.

So every component type will be derived from a single abstract class which will let me store them all in the same vector.

But I do need some way of knowing what kind of components are attached to an entity. So I need a certain way of differentiating as well, and that's where I really need the help!

Here's an example piece of code to very simply illustrate what I'm trying to implement:

    class Component
    {}

    class Rigidbody : public Component
    {}

    class Mesh : public Component
    {}

....

    class ContainerClass
    {
    public:
        template<typename T>
        bool contains(const T element) const
        {
            //Return whether or not elements contains an element of type T.
            //which has to be a class derived from the Component class, 
            //but not of abstract type "Component".

            //I also need a way of making sure T can only be of a type derived from Component.
        }

        inline void add(const Component &p_Component)
        {
            m_Components.push_back(p_Component);
        }

    private:
        vector<Component> m_Components;
    }

So in:

ContainerClass test;
B b;
test.add(b);
  • test.contains(B); Should be true.

  • test.contains(C); Should be false.

By the way, I know there are some similar questions to this one here in StackOverflow, but every single solution I've seen is programming-language specific and doesn't apply to C++.

At least as far as I know.

Thanks!

Ncortiz
  • 21
  • 6
  • 2
    You *could* do that with `dynamic_cast`. – Jesper Juhl Aug 12 '18 at 15:33
  • 1
    You are currently slicing your objects. – Passer By Aug 12 '18 at 15:47
  • What is the type `A` you're storing in `m_Components`? – 1201ProgramAlarm Aug 12 '18 at 15:47
  • My mistake! m_Components is a vector. – Ncortiz Aug 12 '18 at 15:53
  • 1
    *"But I do need some way of knowing what kind of components are attached to an entity."* - If that's true then this may not be the best solution. If you have to test the type of the component a lot, then the benefits of deriving types is being reduced. – Galik Aug 12 '18 at 15:55
  • You could add a member `string type` define by each child and check it's content to know the type – Erwan Aug 12 '18 at 15:57
  • @PasserBy How could I possibly avoid object-slicing while still keeping track of the objects? Even if the original objects (the ones copied into the vector) were deleted. If I keep pointers to every components instead of a copy to the components, then I'll need to keep these components within scope otherwise the pointers within my container-class will end up being dangling-pointers. – Ncortiz Aug 12 '18 at 15:59
  • You typically allocate them and have pointers pointing to them. The owner(s) of the object should be using smart pointers. – Passer By Aug 12 '18 at 16:01
  • @Erwan That would work in most programming languages, but I seemed to ignore the issue of object-slicing that PasserBy mentioned. So keeping a copy of every object won't work. – Ncortiz Aug 12 '18 at 16:03
  • @PasserBy Thanks I'm going to try that... – Ncortiz Aug 12 '18 at 16:10
  • > You typically allocate them and have pointers pointing to them. The > owner(s) of the object should be using smart pointers. – Passer By 9 > mins ago That's what I'm going to try to do. Additionally, I'll create the components derived from the component class within the entity instead of just copying them or creating them and then having pointers/references to them. That will allow me to allocate the instances of the components and then the entity class will have smart-pointers pointing to the components in the heap. – Ncortiz Aug 12 '18 at 16:15
  • 1
    Apart from the object slicing iasue there's a more general (language-independent) problem with your apptoach. *If you want to know the exact type of your polymorphic object, you are doing it wrong*. It is very easy to know what the exact type is (use dynamic_cast or typeid). *It is hard to do the right thing with this information.* – n. m. could be an AI Aug 12 '18 at 16:18

0 Answers0