How to return the class an object belongs to?

I have a base class A. B and C both inherit common variables from A, but B has an array of pointers to B or C objects, and C has a void pointer to an int or a double variable and a type variable to keep track of which.

How do I return which derivative of A (B or C) an object is. In other words, how do I find the class an object belongs to? So I know which is B or C and which functions/variables to access? Does Arduino support C++'s Dynamic casting?

The Arduino is programmed in C++. The gcc compiler is used to compile the code. Anything you can do in C++, you can do on the Arduino.

But, determining which derived class an object was created from, when all you have is a pointer to the base class is not going to be trivial.

The easiest method is to create a pure virtual method in the base class, isType(), that returns the name of the class.
Each class that derives from the base class would then have to implement that method, and return a unique value.

Then, you can ask the object what type it is.

Thanks, that sounds like the most direct approach. I can't help feeling like there is a better object orientated method using function calls etc but this is more simple.

I just don't know how to declare a variable which stores a variable type or class. Is something like type_t what I'm after?

The best way I can think of is using enumerated types, so I will try that in the mean time. If you have a better suggestion please let me know.

See Run-time type information - Wikipedia

DavidOConnor:
See Run-time type information - Wikipedia

Oh, good to know!

DavidOConnor:
See Run-time type information - Wikipedia

Turned off via compiler option

sketch_apr12a.ino: In function 'void setup()':
sketch_apr12a:72: error: cannot use typeid with -fno-rtti

Given the fact that an exception and class name come with it enabling support for it is probably very expensive.

In my experience, PaulS's suggestion works well. As an added bonus the developer gets to decide what information is provided.

I just did instead:

 enum TYPE
 {
   MENU,
   INT,
   DOUBLE
 };

Now after checking for the appropriate type, if it is of type INT or DOUBLE then I need to find the number of characters of a variable of either of those types.

e.g.

float dvalue=2.5;
int nvalue=1469;

dvalue would be 3, nvalue would be 4. I have seen a few solutions on the internet, but which works best for Arduino? So far it appears to be casting it to a String and using strlen but I have been told to stay away from strings, also there doesn't seem to be an easy way to convert a double to string; which I find surprising, and expect even further limitations concerning arduino.

Could dtostrf() solve my problem?

syphex:
Obviously the typeid() function is supposed to work with C++ but it doesn't..

Of course it works. It has been disabled. You have access to all the source code. The source code for Libc, the toolset, and most importantly the Arduino IDE. If you want typeid to be available change the IDE.

Or, use version 1.5. The compiler switches can easily be changed by modifying a text file.

...also dtostrf() would solve my above problem, but according to another forum post on here it is only available on AVR

Have you tried it?

Ok it was an old post when it had not been enabled yet, and no I only just found out about it. I'll give it a shot.