Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » How can static polymorphic objects be stored in a vector?
How can static polymorphic objects be stored in a vector? [message #1864514] Wed, 20 March 2024 17:26 Go to next message
Audrey Fadel is currently offline Audrey FadelFriend
Messages: 2
Registered: March 2024
Junior Member
Hello everyone!

I designed my program using of Curiously recurring template pattern. I have BaseComponent<T> class and about 100 classes that are deriving from it. I want to init objects of these classes in runtime and store these objects in a vector in a specific order. Then go through this vector and call the BaseComponent<T> interface for each of them. Since I can't have vector<BaseComponent*> I added Component class. So the hierarchy is following: Component -> BaseComponent<T> -> other classes. It allows me to keep objects in specific order with vector<Component*>. But then these objects can't be cast to BaseComponent and don't have interface I required. I can use plnkgame virtual function but it kills the whole idea of CRTP (and performance too). I did measurements and in my case, the use of virtual functions is super bad.

In brief: I want to have BaseComponent<T> in a specific order and use that interface without virtual functions. Does someone have advice on how can I achieve that?

[Updated on: Wed, 27 March 2024 13:34]

Report message to a moderator

Re: How can static polymorphic objects be stored in a vector? [message #1864610 is a reply to message #1864514] Thu, 28 March 2024 07:40 Go to previous message
Brovtal Gemini is currently offline Brovtal GeminiFriend
Messages: 2
Registered: October 2023
Junior Member
Hey there!

That's an interesting situation with the CRTP and specific order for derived components. Here are a couple of thoughts:

Consider Alternative Ordering: Could you achieve the desired order without the vector? Maybe a linked list or a custom container that manages order based on your component types?

Interface Inheritance with Static Methods: If order isn't crucial, explore defining the interface methods in the Component class as static functions. Derived classes can then implement them directly. This avoids virtual functions.

CRTP with Friend Classes: Maybe explore using friend classes within the template. This allows derived classes to access private members of BaseComponent<T>, potentially enabling interface calls without virtuals. (Note: This approach can have complexity drawbacks)

Remember, the best approach depends on your specific needs. It might be worth evaluating if a different ordering strategy or a slight interface redesign could avoid the virtual function overhead.
Previous Topic:Fail to start after upgrade to 2024-03
Next Topic:Eclipse Debugger Usage with gcc on WIndows
Goto Forum:
  


Current Time: Sat Dec 14 08:18:37 GMT 2024

Powered by FUDForum. Page generated in 0.02937 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top