Testing type of void*

G

Guest

Hello,

I have some code where I need to store a value as LPVOID. I'd like to be
able to do some runtime testing about whether the thing that's pointed to is
of a certain type. I've tried using RTTI's typeid and dynamic_cast functions
(the latter won't compile), but they've not worked for me. I've included a
short sample program that sort of shows what I'm trying to do and possibly
reveals whether I'm doing something dumb.

The output of the sample program is "I don't know what it is" repeated twice.

Any help would be appreciated,

Thanks,
Notre

#include "stdafx.h"
#include <typeinfo>
#include <stdio.h>

class A
{
public:
A() {m_a = 0;}
void SetA(int a)
{
m_a = a;
}
int GetA()
{
return m_a;
}
private:
int m_a;
};

class B
{
public:
B() {m_b = 0;}
void SetB(int b)
{
m_b = b;
}
int GetB()
{
return m_b;
}
private:
int m_b;
};

void TestTypeId(void* lpThing)
{
if (lpThing != NULL)
{
if (typeid(lpThing) == typeid(A))
printf("It's an A\n");
else if (typeid(lpThing) == typeid(B))
printf("It's a B\n");
else
printf("I don't know what it is.\n");

}
}

void TestDynamicCast(void* lpThing)
{
if (lpThing != NULL)
{
//A* pa = dynamic_cast<A*> (lpThing); //won't compile
//B* pb = dynamic_cast<B*> (lpThing); //won't compile
/*
if (pa != NULL)
printf("It's an A\n");
else if (pb != NULL)
printf("It's a B\n");
else
printf("I don't know what it is.\n");
*/
}
}

int _tmain(int argc, _TCHAR* argv[])
{
A a;
B b;

a.SetA(34);
b.SetB(45);
TestTypeId((void*) &a);
TestTypeId((void*) &b);
TestDynamicCast((void*) &a);
TestDynamicCast((void*) &b);
return 0;
}
 
D

Doug Harrison [MVP]

Hello,

I have some code where I need to store a value as LPVOID. I'd like to be
able to do some runtime testing about whether the thing that's pointed to is
of a certain type. I've tried using RTTI's typeid and dynamic_cast functions
(the latter won't compile), but they've not worked for me. I've included a
short sample program that sort of shows what I'm trying to do and possibly
reveals whether I'm doing something dumb.

There's no way to recover the type from a void*.
 
D

Doug Harrison [MVP]

What the reason behind it..???

The void* type can hold a pointer of any data type, and even if the data
contains some sort of self-identifying tag, it could be a spurious
occurrence of that tag. So the only reliable way to tell what a void*
points to is to remember it yourself. Things are much different in .NET
where everything derives from System.Object, and you can recover type
information when you have an Object reference.
 
T

Tom Widmer [VC++ MVP]

Laxman said:
What the reason behind it..???

For a start, void* doesn't haven't to point to any object. e.g. it could
point to the 3rd byte of a double variable or to the memory location
immediately after an object. e.g.

int i;
void* p = &i + 1; //p doesn't point to any object!

Finally, C++ is an efficient language, and doesn't want to force every
object (even ints and doubles!) to carry around runtime information
about its type - if you want it, you should use your own base class with
virtual functions, or pass around some kind of tag with the void*
pointer to say what it is (a technique commonly used in C).

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top