+ 1

How to make an array that has objects made from different classes in C++

29th Jan 2019, 2:15 PM
darkorbit17493
6 Answers
+ 2
Well for example if you do something like class Base { public: virtual ~Base(){} }; class Derived : public Base { public: DoSomething(){ ... } }; Base* p = new Derived; How does p know what type it is pointing to, how do you know where it is pointing to if you didn't know about the existence of new Derived? How can you call DoSomething from p? p->DoSomething() wouldn't work because Base doesn't have DoSomething as a function. And again, you can't cast it to a Derived because we pretend you don't know what object Base is pointing to. Sure, you can give Base a virtual function and have Derived implement that but you still wouldn't know that it is an object from Derived, this is where identifiers come in. What if we used an enum that tells us the type? class Base { public: enum class Type{ None, Type1, Type2 } Type GetType() const noexcept { return m_Type; } virtual ~Base(){} protected: Type m_Type = Type::None; }; class Derived : public Base { public: // Assign m_Type with a unique enum Derived(){ m_Type = Type::Type1; } void DoSomething(){ ... } }; class Derived2 : public Base { public: Derived2(){ m_Type = Type::Type2; } void DoSomethingElse(){ ... } }; Base* p = new Derived; // Or new Derived2; if( p->GetType() == Base::Type::Type1 ) dynamic_cast<Derived*>( p )->DoSomething(); else if( p->GetType() == Base::Type::Type2 ) dynamic_cast<Derived2*>( p )->DoSomethingElse(); Now we can safely cast to the correct type because we 100% know the correct type. But this is just a basic example, there are better ways of doing this. For example you aren't guaranteed that the type is unique because you can make a mistake in one of the constructors. A really good one used in games is called the Entity Component System but it's a bit too complicated to explain here so I'll just link you to a video: https://www.youtube.com/watch?v=XsvI8Sng6dk You do need to know advanced C++ though.
14th Feb 2019, 7:10 PM
Dennis
Dennis - avatar
+ 2
In short: You don't. What you can do is use polymorphism though. But in order to access the derived class you still need some way of knowing what that type is. You could use an enum for that. You could also use void* and unions, but all of them suffer from the same problem. You need a way to cast it to the correct type. Another way would be to store a string and interpret it as you go. But that can get quite complex and slow, though it is a good way to mix integers and strings. Most of the time though, you really don't need an array that can hold different classes and a design change can fix the need for one.
29th Jan 2019, 5:37 PM
Dennis
Dennis - avatar
+ 2
Yes, that is called polymorphism.
14th Feb 2019, 5:47 PM
Dennis
Dennis - avatar
0
What about an array of objects from different classes that have the same parent class
14th Feb 2019, 5:45 PM
darkorbit17493
0
Can you explain me the first part of your first reply
14th Feb 2019, 6:26 PM
darkorbit17493
0
Thank a lot for the help :)
15th Feb 2019, 9:00 PM
darkorbit17493