Jump to content
  • Advertisement
Sign in to follow this  
RealMarkP

[C++] Getting Pointer Type

This topic is 3605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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 >_>).

Share this post


Link to post
Share on other sites
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 CSerializable
if (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.

Share this post


Link to post
Share on other sites
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().

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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 */ }

// elsewhere
MySerializable a;
int b = 0;

serialize(a);
serialize(b);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!