Arduino class hierarcy - progenitor class

Hello Everyone,

I am trying to store multiple class instances of different types into a linked list:

class Service
{
	public:
		char* fName;
		WhatTypeShouldThisBe fMyVariableInQuestion;
		
		bool fIsInitialized;
		bool fIsRunning;
		Service(char* aName);
};

LinkedList<Service*> fServices = LinkedList<Service*>();

In C# I would use the “System.Object” for “WhatTypeShouldThisBe” and I would store the original type in an additional “System.Type” variable.

Is there any progenitor class in the Arduino C++ framework such as “System.Object” in C#? If not, how can I refer to an object of an uncertain type?

Many thanks in advance.

Are they related or it can be any object type In hierarchy ?

You could always keep the pointers in a void*

Just roll your own.

Some sort of base class that likes to go into a list or something. You could even add an ID to it so you knew what kind of object your looking at.

-jim lee

J-M-L:
Are they related or it can be any object type In hierarchy ?

No, they are not related. That is the problem. I'll give it a try with the void*, see what happens. Thanks for the tip, though. :slight_smile:

You’ll have to cast every time you call a method though so it’s not fantastic

It’s a weird design to have a need to store in a structure an element that can be so diverse.

May be if you tell us more about what you are trying to achieve we could discuss if there is a better way to achieve what you are looking for (templates, friend classes, multiple inheritance, pure virtual functions ...) C++ has lots to offer

ams01:
No, they are not related. That is the problem. I'll give it a try with the void*, see what happens. Thanks for the tip, though. :slight_smile:

I believe objects in C#/Java all share a common root of type System.Object, but that is not the case with C++.
In C++, at bare minimum an array must include objects that share common type.

Your best approach would be creating a base interface class, and implement the functionalities in the concrete classes.
Then you can create a list of the type of the interface class to store the concrete classes. You can use "dynamic_cast" to cast from the base type to the inherited type.