[C++] Getting Pointer Type

Started by
16 comments, last by RealMarkP 15 years, 4 months ago
I have an Array Data Structure that mimics std::vector. In it, I hold values such as classes and primitive types (ints, floats, etc). I was wonder if there is a was to see if the items in the array are of a certain type. I'm also wondering if there is a way to do this without using typeid(). Any suggestions?
------------Anything prior to 9am should be illegal.
Advertisement
You could make a structure that works like a variant.

// this is c++ pseudo code...typedef enum {     vNULL = 0     INTEGER = 1,     FLOAT     = 2,     DOUBLE  = 3} VariantTypes;class myVariant : myType(vNULL){   VariantTypes myType;   union   {        int myInt;        float myFloat;        double myDouble;   }public:    bool operator=(const int &a)    {         myType = INTEGER;         myInt = a;         return TRUE;     }    bool operator=(const float &a)    {         myType = FLOAT;         myIFloat = a;         return TRUE;    }      bool operator=(const double &a)    {         myType = DOUBLE;         myIDouble = a;         return TRUE;    }    VariantTypes GetType()    {      return myType;    }};


The union lets you keep all the elements together, so as to not waste memory.
Whenever you assign a value to the variant, the appropriate overload will be chosen.
It will copy the value, and keep the type.

Variants are great, because you can build arbitrary property sets with them and not worry about typing.
Quote:Original post by Mathucub
You could make a structure that works like a variant.


Hmm, maybe im not explaining this correctly. What I'm making is a serializable array that can serialize anything in it. If the objects it's storing is not derived from CSerializable, then I would want to do a byte dump. Essentially my code looks like this:

if (typeid(val) == typeid(CSerializable)){  // Call its internal serialize function (overloaded)  buf.write(val.serialize())}else{  // This means its a primitive (such as int, float, etc)  buf.write(&val, sizeof(val));}


Or something like that. I would prefer not to use typeid() tho. Your class that you posted is a bit overkill, I think, but i will keep it in mind if all other options are exhausted.
------------Anything prior to 9am should be illegal.
How are you storing the objects in the array?
if (boost::type_traits::is_pod< val_t >::value) {    //Do your primitive stuff } else {    //Do more complicated stuff }


Where val_t is the type of the value (Which you should know >_>).

[size=1]Visit my website, rawrrawr.com

Quote:
How are you storing the objects in the array?

It's a template.


Quote:Original post by bobofjoe
if (boost::type_traits::is_pod< val_t >::value) {    //Do your primitive stuff } else {    //Do more complicated stuff }


Where val_t is the type of the value (Which you should know >_>).


Good idea, but what if the Type is an object that doesn't inherit from CSerializable, it will still fail that test and go into the complicated portion of the if statement.

Is it possible to do this using dynamic_cast<> or any type of casting mechanism that can throw an exception if it cannot cast??

ie:
CMyClass val; // not from CSerializableif (boost::type_traits::is_pod<val>::value) {...}


Basically, if it's not derived from CSerializable or if its a Primitive, then I will do one thing, else if it is derived from CSerializable, I want it to do something else.

Could you use dynamic_cast<>() or any similar function to check if it can be converted to type CSerializable? Throwing/catching exceptions is fine.
------------Anything prior to 9am should be illegal.
Quote:Original post by RealMarkP
Could you use dynamic_cast<>() or any similar function to check if it can be converted to type CSerializable? Throwing/catching exceptions is fine.


Yes. dynamic_cast<CSerializable&>(val) will throw an exception. It's only a few times more costly than typeid().

Stephen M. Webb
Professional Free Software Developer

Quote:Original post by Bregma
Quote:Original post by RealMarkP
Could you use dynamic_cast<>() or any similar function to check if it can be converted to type CSerializable? Throwing/catching exceptions is fine.


Yes. dynamic_cast<CSerializable&>(val) will throw an exception. It's only a few times more costly than typeid().


Hmm, how would I go about it throwing an exception?

int *val;
CSerializable *s = dynamic_cast<CSerializable *>(val);

This doesn't compile. (Obviously, I almost never use c++ style casting).
------------Anything prior to 9am should be illegal.
Correct me if I'm wrong but in C++0x there's going to be a typeof operator.
Here to learn :)
Quote:Original post by RealMarkP]
I have an Array Data Structure that mimics std::vector
Why not use std::vector?

Quote:Original post by RealMarkP
<code here>
Or something like that. I would prefer not to use typeid() tho.
What's the reasoning for avoiding typeid?


I don't know what type val is, could you just use overloads to take care of the type matching?:

void serialize(CSerializable & s) { /* handle CSerializable */ }template < typename T >void serialize(T x) { /* handle non-CSerializable */ }// elsewhereMySerializable a;int b = 0;serialize(a);serialize(b);

This topic is closed to new replies.

Advertisement